我已经用python编写了一个脚本来刮取表名为England
下的所有链接,然后在脚本到达内页时使用这些链接,它将刮取下一页链接。我知道如果我修复脚本中使用的XPath,我可能会得到唯一的下一页URL。你知道吗
但是,这里的主要目标是确定为什么我的脚本即使在使用set()
时也会产生重复。你知道吗
我的剧本:
import requests
from lxml.html import fromstring
from urllib.parse import urljoin
link = "http://tennishub.co.uk/"
processed_links = set()
processed_nextpage_links = set()
def get_links(url):
response = requests.get(url)
tree = fromstring(response.text)
unprocessed_links = [urljoin(link,item.xpath('.//a/@href')[0]) for item in tree.xpath('//*[@class="countylist"]')]
for nlink in unprocessed_links:
if nlink not in processed_links:
processed_links.add(nlink)
get_nextpage_links(processed_links)
def get_nextpage_links(itemlinks):
for ilink in itemlinks:
response = requests.get(ilink)
tree = fromstring(response.text)
titles = [title.xpath('.//a/@href')[0] for title in tree.xpath('//div[@class="pagination"]') if title.xpath('.//a/@href')]
for ititle in titles:
if ititle not in processed_nextpage_links:
processed_nextpage_links.add(ititle)
for rlink in processed_nextpage_links:
print(rlink)
if __name__ == '__main__':
get_links(link)
结果我发现:
/tennis-clubs-by-county/Durham/2
/tennis-clubs-by-county/Durham/2
/tennis-clubs-by-county/Durham/2
/tennis-clubs-by-county/Cheshire/2
/tennis-clubs-by-county/Derbyshire/2
/tennis-clubs-by-county/Durham/2
/tennis-clubs-by-county/Cheshire/2
/tennis-clubs-by-county/Derbyshire/2
/tennis-clubs-by-county/Durham/2
请尝试以下脚本。结果发现,你的xapth在解析一个特定的块时有一些缺陷,正如@tripleee在他的评论中已经提到的那样。我在follong脚本中使用
set()
的方式略有不同。现在,它应该产生独特的联系。你知道吗每次调用
get_nextpage_links
时,都会打印到目前为止收集的所有链接。你知道吗我想您应该完全删除
print
,完成后只打印列表,最好是在任何def
之外(使函数可重用,并将任何外部副作用推迟到调用代码)。你知道吗没有全局变量的更好的解决方案可能是让
get_links
收集一个集合并返回它,无论何时调用它,都将对集合的引用传递给get_nextpage_links
,并且(显然)让它添加任何新链接。你知道吗因为您使用的是集合,所以在添加链接之前,不需要特别检查链接是否已经在集合中。无法将副本添加到此数据类型。你知道吗
每次你打电话的时候
打印它是因为for循环在for循环中,在集合中添加链接
相关问题 更多 >
编程相关推荐