因此,由于我需要更详细的数据,我必须在网站的HTML代码中进行更深入的挖掘。我编写了一个脚本,返回到详细页面的特定链接列表,但我无法使用Python来搜索该列表中的每个链接,它总是在第一个链接处停止。我做错什么了?你知道吗
from BeautifulSoup import BeautifulSoup
import urllib2
from lxml import html
import requests
#Open site
html_page = urllib2.urlopen("http://www.sitetoscrape.ch/somesite.aspx")
#Inform BeautifulSoup
soup = BeautifulSoup(html_page)
#Search for the specific links
for link in soup.findAll('a', href=re.compile('/d/part/of/thelink/ineed.aspx')):
#print found links
print link.get('href')
#complete links
complete_links = 'http://www.sitetoscrape.ch' + link.get('href')
#print complete links
print complete_links
#
#EVERYTHING WORKS FINE TO THIS POINT
#
page = requests.get(complete_links)
tree = html.fromstring(page.text)
#Details
name = tree.xpath('//dl[@class="services"]')
for i in name:
print i.text_content()
另外:你能给我推荐什么教程来学习如何把我的输出放到一个文件中,并清理它,给出变量名,等等?你知道吗
我认为您需要的是
complete_links
中的链接列表,而不是单个链接。正如@Pynchia和@lemonhead所说的,您正在覆盖first for循环的每个迭代。你知道吗您需要两个更改:
将链接附加到列表,并使用它循环和废弃每个链接
在另一个循环中废弃每个累积的链接
附言:我推荐scrapy framework这样的任务。你知道吗
相关问题 更多 >
编程相关推荐