我是网络抓取的新手。我正在试着刮掉标题(QCY T5无线蓝牙耳机V5.0触控立体声高清通话,380mAh电池)使用BeautifulSoup,但它向我展示了无在输出中。下面是我尝试过的代码:
from bs4 import BeautifulSoup
import requests
page=requests.get('https://www.daraz.pk/products/qcy-t5-wireless-bluetooth-earphones-v50-touch-control-stereo-hd-talking-with-380mah-battery-i143388262-s1304364361.html?spm=a2a0e.searchlist.list.1.5b7c4a71Jr4QZb&search=1')
soup=BeautifulSoup(page.content,'html.parser')
print (page.status_code)
heading=soup.find(class_='pdp-mod-product-badge-title')
print(heading)来自网站的html代码:
QCY T5 Wireless Bluetooth Earphones V5.0 Touch Control Stereo HD talking with 380mAh battery
发布于 2020-08-06 18:40:26
page.content中没有"pdp-mod-product-badge-title“,正确的类应该是"breadcrumb”。_项目_锚点_最后“,您可以在浏览器的View Source中将其提取出来。

代码:
from bs4 import BeautifulSoup
import requests
page=requests.get('https://www.daraz.pk/products/qcy-t5-wireless-bluetooth-earphones-v50-touch-control-stereo-hd-talking-with-380mah-battery-i143388262-s1304364361.html?spm=a2a0e.searchlist.list.1.5b7c4a71Jr4QZb&search=1')
soup=BeautifulSoup(page.content,'html.parser')
print (page.status_code)
heading=soup.find(class_='breadcrumb_item_anchor_last')
print(heading.text.strip()) #Thanks to @bigbounty发布于 2020-08-06 18:31:10
您无法获取数据的原因是因为网站的View Source没有您提到的类。
初学者所犯的一个基本错误是在页面的Inspect选项卡中查找元素并为其标识类
抓取..。永远不要这样做。
为了保证所有数据的可靠性,请始终使用Ctrl +U转到View Source of the page,然后查找您的内容。在大多数情况下,内容是通过使用JS文件和API调用动态呈现的,这些API调用可以从网络选项卡中找到。
对于上面的问题,您正在寻找的信息也是动态加载的,并且在页面的源代码中不可用。
https://stackoverflow.com/questions/63281539
复制相似问题