各位大lao,我这里是因为什么报错呀
# 导包
import requests # 发送网络请求的工具包
from lxml import etree # 转换数据类型
headers = {
# 模拟游览器
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36"
}
# 设置多个网页,预留更改位置
url = 'https://www.autohome.com.cn/grade/carhtml/%d.html'
# 设置A~Z的选项网页
for i in range(ord("A"), ord("Z") + 1):
U = chr(i)
new_url = "https://www.autohome.com.cn/grade/carhtml/%s.html" % U # 字符串拼接A-Z字母
# new_url = format(url%i)
respone = requests.get(url=new_url, headers=headers) # 发送请求
# print(respone.text)
# 提取我们想要的数据
at = etree.HTML(respone.text)
# print(html_data)
new_url_list = at.xpath('//ul[@class="rank-list-ul"]/li/div/a[@class="red"]/@href')
print(new_url_list)
```python
报错:
Traceback (most recent call last):
File "C:\Users\Administrator\PycharmProjects\pythonProject\qichezhijia.py", line 22, in <module>
new_url_list = at.xpath('//ul[@class="rank-list-ul"]/li/div/a[@class="red"]/@href')
AttributeError: 'NoneType' object has no attribute 'xpath'
```
没有u这个网页
for letter in range(65,91):
if chr(letter)=='U':
continue
new_url = f"https://www.autohome.com.cn/grade/carhtml/{chr(letter)}.html" # 字符串拼接A-Z字母
该回答内容部分引用GPT,GPT_Pro更好的解决问题
这里出现的AttributeError: 'NoneType' object has no attribute 'xpath'错误,是由于你在第17行尝试使用xpath方法对respone对象进行操作时,却发现它是一个NoneType对象,而NoneType对象没有xpath属性,所以报错;
因此,要解决这个问题,需要先检查requests.get()的返回值是否正确,能够正确的获取到网页内容;
如果获取正确的网页内容,可以使用如下代码:
for i in range(ord("A"),ord("Zz") + 1):
U = chr(i)
new_url = "%U"
respone = requests.get(url-new_url,headers=headers)
html = etree.HTML(respone.text)
# 可以使用html的xpath方法进行操作
如果获取不到正确的网页内容,可以使用下面的代码:
for i in range(ord("A"),ord("Zz") + 1):
U = chr(i)
new_url = "%U"
respone = requests.get(url-new_url,headers=headers)
if respone.status_code == 200: # 判断返回状态
html = etree.HTML(respone.text)
# 可以使用html的xpath方法进行操作
最后,一定要检查requests.get()的返回值是否正确,才能保证正确解决AttributeError: 'NoneType' object has no attribute 'xpath'这个问题。
如果回答有帮助,望采纳。
以下答案引用自GPT-3大模型,请合理使用:
示例
这里可能是你的respone.text没有获取到内容,导致etree.HTML解析出来的at是None,因此就没有xpath属性,请检查你发送请求的网址是否正确,url应该是那个new_url:
url = 'https://www.autohome.com.cn/grade/carhtml/%d.html'
# 设置A~Z的选项网页
for i in range(ord("A"), ord("Z") + 1):
U = chr(i)
new_url = "https://www.autohome.com.cn/grade/carhtml/%s.html" % U # 字符串拼接A-Z字母
respone = requests.get(url=new_url, headers=headers) # 发送请求
完整代码示例:
# 导包
import requests # 发送网络请求的工具包
from lxml import etree # 转换数据类型
headers = {
# 模拟游览器
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36"
}
# 设置多个网页,预留更改位置
url = 'https://www.autohome.com.cn/grade/carhtml/%d.html'
# 设置A~Z的选项网页
for i in range(ord("A"), ord("Z") + 1):
U = chr(i)
new_url = "https://www.autohome.com.cn/grade/carhtml/%s.html" % U # 字符串拼接A-Z字母
# new_url = format(url%i)
respone = requests.get(url=new_url, headers=headers) # 发送请求
# 判断请求成功
if respone.status_code == 200:
# 提取我们想要的数据
at = etree.HTML(respone.text)
# print(html_data)
new_url_list = at.xpath('//ul[@class="rank-list-ul"]/li/div/a[@class="red"]/@href')
print(new_url_list)
else:
print('error')
如果我的回答解决了您的问题,请采纳我的回答
不知道你这个问题是否已经解决, 如果还没有解决的话: