python - 摆脱重复链接的麻烦

标签 python web-scraping duplicates web-crawler

尝试了很多不同的链接,但每次我都得到相同的结果:第一个链接总是最后一次结束。

import requests
from lxml import html
Unique=[]
url="https://www.yellowpages.com/search?search_terms=coffee&geo_location_terms=Los+Angeles%2C+CA"
def DupRemoval(Address):
    MainLink="https://www.yellowpages.com"
    response = requests.get(Address)
    Unique.append(Address)
    tree=html.fromstring(response.text)
    Pagination_link=tree.xpath("//div[@class='pagination']//a/@href")
    for Nextpage in Pagination_link:
        Blink=MainLink+Nextpage
        if Blink not in Unique:
            print(Blink)

DupRemoval(url)

生成的链接:

enter image description here

最佳答案

重复链接是“下一步”链接按钮,它是分页 block 中的最后一个。此外,如果您进一步前进到下一页,您还会在那里获得“上一页”链接。

过滤掉它的一种快速方法是获取所有没有 class 属性的 a 元素:

//div[@class='pagination']//a[not(@class)]/@href

关于python - 摆脱重复链接的麻烦,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43958159/

相关文章:

python - 网页抓取 : How to extract just the Information that I need

python : Submit a form on website using python

具有非重复二项式的 R data.frame

postgresql - 获取两行之间不同的列

python - 在 flask 模板中访问这些字典值

python - 如何获取正在使用的 ipython 笔记本的文件路径? (相当于 __file__)

python - 如何使用 Python 每天抓取一次每日新闻?

python - 奇怪的 ~150ms 启动惩罚使用 python setuptools

python - 使用 Python 更改 URL 中的查询

正则表达式从逗号分隔列表中删除连续的重复项(整数和/或 float )