python - Python抓取bs4 TypeError:'NoneType'对象不可下标
问题描述
我希望你很好。您能否告诉我为什么我可以正确使用我的抓取脚本:) 它适用于其他网站。我是初学者,所以我可能犯了一个基本错误
import requests
from bs4 import BeautifulSoup
import time
import csv
links = []
for i in range(1):
url = '*******/recettes/?page={}' + str(i)
res = requests.get(url,headers={'User-Agent': 'Mozilla/5.0'})
response = requests.get(url)
print(response)
if response.ok:
print('Page: ' + str(i))
soup = BeautifulSoup(response.text, "html.parser")
divs = soup.findAll('div', class_ = 'field-item even')
for div in divs:
a = div.find('a')
link = a['href']
links.append('*******' + link)
time.sleep(3)
print(len(links))
with open('urls3.txt', 'w') as file:
for link in links:
file.write(link + '\n')
"""
with open('urls3.txt', 'r') as inf:
with open('recipes3.csv', 'w') as outf:
outf.write('titre,image,url,\n')
for row in inf:
url = row.strip()
response = requests.get(url)
if response.ok:
soup = BeautifulSoup(response.text, "html.parser")
titre = soup.find('h1')
image = soup.find('img', {"id":"recipe-media-viewer-thumbnail-1"})['src']
print(titre.text, image, url)
outf.write(str(titre.text) + ',' + str(image) + ',' + str(url) + '\n')
time.sleep(1)
"""
你能告诉我为什么这里有错误:
<Response [200]>
Page: 0
Traceback (most recent call last):
File "ex3.py", line 18, in <module>
link = a['href']
TypeError: 'NoneType' object is not subscriptable
解决方案
我找到了答案,我把它贴在这里:) 给任何有兴趣的人
try:
image = soup.find('img', {"id":"recipe-media-viewer-thumbnail-1"})['src']
except Exception as e:
image = None
推荐阅读
- r - “有非零退出状态”错误,如何解决?
- matlab - 在matlab中创建潜在涡旋的模拟
- python - 常用表达
- android - 如何使用 WindowInsets API(兼容版)显示/隐藏键盘?
- python - 具有多个标记的分组条形图
- api - 使用 Google API 禁用 Chrome 设备
- c++ - 修改 int** 2d 数组中的值,该数组是另一个成员变量的成员变量 [C++]
- ruby-on-rails - 无法使用脚本验证动态添加的 CSRF 令牌真实性
- c++ - C++模板函数中静态变量的多个实例
- ubuntu-20.04 - 重启主服务器后,Pacemaker 上的故障转移 IP 设置为从属