去南极看鲸鱼 2023-02-22 15:06 采纳率: 33.3%
浏览 287

AttributeError: 'NoneType' object has no attribute 'xpath'

各位大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'

```

  • 写回答

4条回答 默认 最新

  • cjh4312 2023-02-22 15:27
    关注

    没有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字母
        
    
    评论 编辑记录

报告相同问题?

问题事件

  • 创建了问题 2月22日