python爬虫记录

import requests
from lxml import html
header = {
        "Connection": "close",
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.106 Safari/537.36"}
etree = html.etree
response = requests.get("http://cansang.gxnongmu.com/docList.aspx?catId=183&Page=1",headers = header)
result = response.content.decode('utf8')
html_result = etree.HTML(result)
Sericulture_titles = html_result.xpath("//div[@class='zt2listnr']/ul[1]/li")
for Sericulture_title in Sericulture_titles:
        Sericulture_title_name = Sericulture_title.xpath("./a/text()")
        Sericulture_title_href = Sericulture_title.xpath("./a/@href")
        print(Sericulture_title_name)
        print(Sericulture_title_href)

在这个过程中遇到两个问题:
问题一:
requests.exceptions.ProxyError: HTTPConnectionPool(host=‘127.0.0.1’, port=8888): Max retries exceeded with url:
网上查了一下是代理的问题。
解决:将代理关闭
在这里插入图片描述
问题二:
requests.exceptions.ConnectionError: (‘Connection aborted.’, RemoteDisconnected('Remote end…
原因:request的连接数过多而导致Max retries exceeded
在header中不使用持久连接,在headers中加入以下代码:

'Connection': 'close'
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值