【问题标题】:I want to crawl data from 1 to 10 pages automatically from website.How can i do it?我想从网站自动抓取 1 到 10 页的数据。我该怎么做?
【发布时间】:2016-10-17 10:10:58
【问题描述】:
import requests
from bs4 import BeautifulSoup
My_Url = "http://questions.consumercomplaints.in/page/2"
Data = requests.get(My_Url)
Soup = BeautifulSoup(Data.content)
head_id = Soup.find_all({"div":"href"})
len(head_id)
for i in head_id:
    print i.text 

从上面的代码中,我从网页 2 中删除(评论/投诉)。 我如何自动抓取所有页面的数据(http://questions.consumercomplaints.in/page/3

【问题讨论】:

    标签: python python-2.7 web-scraping web-crawler ipython


    【解决方案1】:

    为什么不在一个范围内的 for 循环中包围你的函数呢?

    import requests
    from bs4 import BeautifulSoup
    for i in range(3,11):
        My_Url = "http://questions.consumercomplaints.in/page/" + str(i)
        Data = requests.get(My_Url)
        Soup = BeautifulSoup(Data.content)
        head_id = Soup.find_all({"div":"href"})
        len(head_id)
        for i in head_id:
            print i.text 
    

    看看范围函数是如何工作的here

    【讨论】:

    • 收到错误“无法连接 'str' 和 'int' 对象”
    • 只需将 i 类型转换为字符串。我已经更新了答案。
    • 我可以投票赞成,由声望低于 15 的人投票:(
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-05
    • 2019-07-28
    • 2013-10-23
    • 2015-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多