请耐心等待,我对Python还很陌生 - 但非常开心。我正在尝试编写一个网络爬虫,从最近的丹麦公投结果中爬取数据。我已经成功地从主页提取了所有相关链接。现在我想让Python跟踪这92个链接,并从每个页面收集9个信息。但我卡住了。希望你能给我一点提示。
以下是我的代码:
以下是我的代码:
import requests
import urllib2
from bs4 import BeautifulSoup
# This is the original url http://www.kmdvalg.dk/
soup = BeautifulSoup(urllib2.urlopen('http://www.kmdvalg.dk/').read())
my_list = []
all_links = soup.find_all("a")
for link in all_links:
link2 = link["href"]
my_list.append(link2)
for i in my_list[1:93]:
print i
# The output shows all the links that I would like to follow and gather information from. How do I do that?
lxml
,它基本上会遍历每个HTML标记,这可以通过在简单的Web浏览器上进行元素检查找到。/text()
将抓取标记内的文本。 - titipata