【问题标题】:Why can't I crawl this link in Python?为什么我无法在 Python 中抓取此链接?
【发布时间】:2015-11-11 11:00:15
【问题描述】:

我正在尝试抓取网页的内容,但我不明白为什么会出现此错误:http.client.IncompleteRead: IncompleteRead(2268 bytes read, 612 more expected)

这是我要抓取的链接: www.rc2.vd.ch

这是我用来抓取的 Python 代码:

import requests
from bs4 import BeautifulSoup
def spider_list():
    url = 'http://www.rc2.vd.ch/registres/hrcintapp-pub/companySearch.action?lang=FR&init=false&advancedMode=false&printMode=false&ofpCriteria=N&actualDate=18.08.2015&rowMin=0&rowMax=0&listSize=0&go=none&showHeader=false&companyName=&companyNameSearchType=CONTAIN&companyOfsUid=&companyOfrcId13Part1=&companyOfrcId13Part2=&companyOfrcId13Part3=&limitResultCompanyActive=ACTIVE&searchRows=51&resultFormat=STD_COMP_NAME&display=Rechercher#result'

    source_code = requests.get(url)
    plain_text = source_code.text
    soup = BeautifulSoup(plain_text, 'html.parser')

    for link in soup.findAll('a', {'class': 'hoverable'}):
        print(link)

spider_list()

我尝试了另一个网站链接,它工作正常,但为什么我不能抓取这个?

如果无法使用此代码执行此操作,我该怎么做?

------------ 编辑 ------------

这是完整的错误信息:

    Traceback (most recent call last):
  File "C:/Users/Nuriddin/PycharmProjects/project/a.py", line 19, in <module>
    spider_list()
  File "C:/Users/Nuriddin/PycharmProjects/project/a.py", line 12, in spider_list
    source_code = requests.get(url)
  File "C:\Python34\lib\site-packages\requests\api.py", line 69, in get
    return request('get', url, params=params, **kwargs)
  File "C:\Python34\lib\site-packages\requests\api.py", line 50, in request
    response = session.request(method=method, url=url, **kwargs)
  File "C:\Python34\lib\site-packages\requests\sessions.py", line 465, in request
    resp = self.send(prep, **send_kwargs)
  File "C:\Python34\lib\site-packages\requests\sessions.py", line 605, in send
    r.content
  File "C:\Python34\lib\site-packages\requests\models.py", line 750, in content
    self._content = bytes().join(self.iter_content(CONTENT_CHUNK_SIZE)) or bytes()
  File "C:\Python34\lib\site-packages\requests\models.py", line 673, in generate
    for chunk in self.raw.stream(chunk_size, decode_content=True):
  File "C:\Python34\lib\site-packages\requests\packages\urllib3\response.py", line 303, in stream
    for line in self.read_chunked(amt, decode_content=decode_content):
  File "C:\Python34\lib\site-packages\requests\packages\urllib3\response.py", line 450, in read_chunked
    chunk = self._handle_chunk(amt)
  File "C:\Python34\lib\site-packages\requests\packages\urllib3\response.py", line 420, in _handle_chunk
    returned_chunk = self._fp._safe_read(self.chunk_left)
  File "C:\Python34\lib\http\client.py", line 664, in _safe_read
    raise IncompleteRead(b''.join(s), amt)
http.client.IncompleteRead: IncompleteRead(4485 bytes read, 628 more expected)

【问题讨论】:

  • 在 python 2 上运行良好
  • @VincentBeltman 为什么它不能在 python 3 上运行?
  • 您能提供完整的堆栈跟踪吗?你如何运行你的程序?
  • @VincentBeltman 我正在通过 Pycharm 运行该程序,我将在我的帖子中发布完整的错误。
  • 无论如何,Q 的第二部分的答案是网络服务器是 b0rken,响应的 Content-Length 标头包含比实际响应中更多的字节

标签: python beautifulsoup web-crawler


【解决方案1】:

可能你的编辑器有问题。

我在 python 3 中得到正确的结果,您的代码在 IDLE

下面附上图片供参考-

我唯一能想到的就是以某种方式绕过错误:

import requests
from bs4 import BeautifulSoup
def spider_list():
    url = 'http://www.rc2.vd.ch/registres/hrcintapp-pub/companySearch.action?lang=FR&init=false&advancedMode=false&printMode=false&ofpCriteria=N&actualDate=18.08.2015&rowMin=0&rowMax=0&listSize=0&go=none&showHeader=false&companyName=&companyNameSearchType=CONTAIN&companyOfsUid=&companyOfrcId13Part1=&companyOfrcId13Part2=&companyOfrcId13Part3=&limitResultCompanyActive=ACTIVE&searchRows=51&resultFormat=STD_COMP_NAME&display=Rechercher#result'
    try:
        source_code = requests.get(url)
        plain_text = source_code.text
        soup = BeautifulSoup(plain_text, 'html.parser')

        for link in soup.findAll('a', {'class': 'hoverable'}):
            print(link)
    except:
        pass
        #I am passing but you do whatever you want to do in case of error
spider_list()

如果有帮助,请告诉我。

【讨论】:

  • 它也不适用于 IDLE,我遇到了同样的错误:http.client.IncompleteRead: IncompleteRead(907 bytes read, 1867 more expected)
  • 如果您尝试其他网站会怎样?
  • @VineetKumarDoshi 不,它没有帮助:/ 我得到同样的错误,我会尝试另一台计算机,如果它有效,我将比较两台计算机上的所有内容
  • 我重新编辑了代码,试试这个。如果这不起作用,请告诉我。
  • 是的,它现在可以在出现错误的情况下工作,但是我的函数应该废弃 url 数据,如果我什么也没得到,我的项目就没有意义了:/ 现在我在另一台计算机上尝试了一切正常我想这是因为这台电脑已经 11 年了,上面的所有东西都被修改了。但是如果出现错误,您的代码会很有用,这就是为什么我会确认这个答案:)
【解决方案2】:

这个怎么样!!

import requests
from lxml.html import fromstring

url = 'https://www.rc2.vd.ch/registres/hrcintapp-pub/companySearch.action?lang=FR&init=false&advancedMode=false&printMode=false&ofpCriteria=N&actualDate=18.08.2015&rowMin=0&rowMax=0&listSize=0&go=none&showHeader=false&companyName=&companyNameSearchType=CONTAIN&companyOfsUid=&companyOfrcId13Part1=&companyOfrcId13Part2=&companyOfrcId13Part3=&limitResultCompanyActive=ACTIVE&searchRows=51&resultFormat=STD_COMP_NAME&display=Rechercher#result'

def spider_list(link):
    code = requests.get(link)
    tree = fromstring(code.text)
    skim = tree.xpath('//a[@class="hoverable"]/@href')
    print(skim)

if __name__ == '__main__':
    spider_list(url)

【讨论】:

    猜你喜欢
    • 2015-01-22
    • 2021-11-22
    • 1970-01-01
    • 1970-01-01
    • 2021-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多