python - 抓取网站的所有列表,超出页面限制

标签 python web-scraping data-science

我正在尝试抓取一个类似于 Yelp 的食品评论网站。我已经完成了抓取单个页面并获取单个餐厅的信息的部分。但是我遇到了这个问题,网站有超过 90 万个列表,但页码最多只有 60 个,最多显示 1200 个列表。即使我缩小过滤器并通过每个过滤器选项,每个过滤器下的列表仍然会超过 1200。
我正在使用 requests 和 beautifulsoup 进行抓取。
有更好或更有效的解决方案的想法吗?

def crawl_listing(url):
    response = requests.get(url)
    soup = BeautifulSoup(response.content, "html.parser")
    #code for obtaining url of each listing on this page
    return (#dict of restaurant names and urls)

def crawl_detail(url):
    response = requests.get(url)
    soup = BeautifulSoup(response.content, "html.parser")
    #code for getting all needed information about the restaurant

def main():
    full_list = []
    for page in range(30):
        address = f"https://www.example.com/list/{page}"
        full_list = full_list + crawl_listing(address)

    for restaurant in full_list:
        crawl_detail(restaurant['url'])

最佳答案

这完全取决于站点(因此您必须分享您实际抓取的站点以获得更多帮助),但大多数情况下,站点显示的内容并不是抓取它的唯一方法(也许可能会有更多的分页)站点中显示的限制)。

另一种选择是检查该站点是否有 robots.txt应该在 https://www.example.com/robots.txt 中找到的文件从那里您可以查看是否有一些正常导航无法遇到的链接。

最后一个真正的建议是使用网络爬虫框架(我推荐 scrapy ),因为如果您访问该网站的次数太多,您将面临很多问题,因为您的请求速度太快.该框架可以帮助您设置一些延迟并根据某些规则自动重试,因此当您面对那些常见的爬行挑战时,它将使您的生活更轻松。

关于python - 抓取网站的所有列表,超出页面限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59242514/

相关文章:

python - 在不改变宽度的情况下减少条之间的间距

javascript - 数组索引在 $.get 中不起作用

plot - 可视化以下数据的最佳方法是什么?

python - 在 python pandas 中,如何保存 'grid chart' ?

python - py2neo,neo4j : How to create relation between two existing node

python - 用python从文件创建字典

python - self.Bind 上的 wxPython 方法 "takes 1 positional argument but 2 were given"

vba - 使用 Excel vba 抓取网站

php - 抓取网页内容

python - 在 pandas 数据框中如何应用自己的函数多选择列并为该计算创建一个新列