【发布时间】:2021-04-08 19:15:20
【问题描述】:
我正在尝试抓取网页清单,但问题是它们没有出现在我的 Python 脚本的输出中
这是出现在导航器上的原始标签,带有我要抓取的文本:
<span class="currentInv">251</span>
" in stock"
这是使用 beautifulsoup 作为库和 lxml 作为解析器解析后的标签,我什至尝试了其他解析器,例如 html.parser 和 html5lib:
<span class="currentInv"></span>
这是我的完整 Python 脚本:
import requests
from bs4 import BeautifulSoup as bs
url = f'https://www.hancocks.co.uk/buy-wholesale-sweets?warehouse=1983&p=1'
parser = 'lxml'
headers = {'User-Agent' : 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36'}
response = requests.get(url, headers=headers)
data = response.text
soup = bs(data, parser)
print(soup.find('span', class_ = 'currentInv').text)
输出为空
我反复尝试了很多次,但似乎没有什么对我有用
任何帮助将不胜感激。
【问题讨论】:
标签: python python-3.x web-scraping beautifulsoup python-requests