【问题标题】:Web crawler not fetching all URLs网络爬虫未获取所有 URL
【发布时间】:2015-07-31 22:15:15
【问题描述】:

所以我编写了以下程序来从这个搜索结果页面https://www.ohiobar.org/Pages/Find-a-Lawyer.aspx?sFN=&sLN=&sPA=&sCI=&sST=OH&sZC=获取所有配置文件 URL

大约有 18,400 多个链接要提取。

但是,当我运行代码时,它不会超出 URL# 1623 并且它会停止而没有给出任何错误或任何东西。

这是我的代码

from bs4 import BeautifulSoup
import requests

url = 'https://www.ohiobar.org/Pages/Find-a-Lawyer.aspx?sFN=&sLN=&sPA=&sCI=&sST=OH&sZC='

with requests.Session() as session:
    session.headers = {'User-Agent': 'Mozilla/5.0 (Linux; U; Android 4.0.3; ko-kr; LG-L160L Build/IML74K) AppleWebkit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30'}

    response = session.get(url)
    soup = BeautifulSoup(response.content, "lxml")

    for link in soup.select("div#content_findResults div#content_column1 ul li a[href*=MemberProfile]"):
        print 'https://www.ohiobar.org' + link.get("href")

请建议我在这里做错了什么?

谢谢

【问题讨论】:

    标签: python-2.7 web-scraping request beautifulsoup web-crawler


    【解决方案1】:

    由于我还不能发表评论,我将添加此作为答案。我尝试在 Python-3.4 上运行您的代码,这就是我得到的:

    如果可能的话,你也许只是更新你的 python 版本。

    在这一行做了一个小改动:

    soup = BeautifulSoup(response.content)
    

    代码:

    from bs4 import BeautifulSoup
    import requests
    
    url = 'https://www.ohiobar.org/Pages/Find-a-Lawyer.aspx?sFN=&sLN=&sPA=&sCI=&sST=OH&sZC='
    
    with requests.Session() as session:
        session.headers = {'User-Agent': 'Mozilla/5.0 (Linux; U; Android 4.0.3; ko-kr; LG-L160L Build/IML74K) AppleWebkit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30'}
    
        response = session.get(url)
        soup = BeautifulSoup(response.content)
        counter = 0
    
        for link in soup.select("div#content_findResults div#content_column1 ul li a[href*=MemberProfile]"):
            print(counter , ": " , 'https://www.ohiobar.org' , link.get("href"))
            counter += 1
    

    问候, 亚历克斯

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-23
      • 2011-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多