解析器不起作用,我不明白问题是什么,以前一切正常,但现在它给出了错误:
Traceback (most recent call last):
File "123.py", line 36, in <module>
parse()
File "123.py", line 30, in parse
img = get_image(html)
File "123.py", line 16, in get_image
soup = BeautifulSoup(html, 'html.parser')
File "C:\Users\musix\AppData\Local\Programs\Python\Python38\lib\site-packages\bs4\__init__.py", line 307, in __init__
elif len(markup) <= 256 and (
TypeError: object of type 'Response' has no len()
这是代码本身:
import requests
from bs4 import BeautifulSoup
import csv
import os
URL = 'https://ptk-svarka.ru/catalog/apparaty-poluavtomaticheskoy-svarki-mig'
HEADERS = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36', 'accept': '*/*'}
FILE = 'svarka.csv'
def get_html(url, params=None):
r = requests.get(url, headers=HEADERS, params=params)
return r
def get_image(html):
soup = BeautifulSoup(html, 'html.parser')
items = soup.find_all('div', class_='b-products')
image = []
for img in image:
image.append(
item.find('a', class_='b-products__text').get('href')
)
return image
def parse():
URL = input('Введите URL: ')
URL = URL.strip()
html = get_html(URL)
if html.status_code == 200:
img = get_image(html)
print(img)
else:
print('Error')
parse()
您
BeautifulSoup
需要将字符串、字节或文件对象传递给,并且您传递了一个requests.Response
.我建议您给参数起正确的名称,并在函数中指定参数的类型和返回值,这不会强制解释器检查类型,但会对 IDE 和程序员有所帮助。
例子: