Python网络垃圾

2024-04-25 16:55:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在Anaconda提示符下通过Python进行web抓取。详情如下

网页垃圾链接:

https://sa.aqar.fm/%D9%81%D9%84%D9%84-%D9%84%D9%84%D8%A8%D9%8A%D8%B9/1

我试图找到标题与下面的代码从上面的网页链接与下面的代码,但不知何故,这给了我一个问题

####脚本:对于容器中的容器:

title_container = container.findAll("div",{"class":"title"})

title_name = title_container[0].text

####错误

File Webscrap.py, line 75, in

title_name = title_container[0].text

IndexError: list index out of range

我已经附上了从上面的链接,我应该提取标题的图像

enter image description here


Tags: 代码textnamehttpsweb网页标题title
1条回答
网友
1楼 · 发布于 2024-04-25 16:55:18

由于语言对我来说是一个严重的障碍,我仍然不确定我到底解析了什么。然而,这应该能让你达到目的

import requests
from bs4 import BeautifulSoup

URL = 'https://sa.aqar.fm/%D9%81%D9%84%D9%84-%D9%84%D9%84%D8%A8%D9%8A%D8%B9/1'

res = requests.get(URL)
soup = BeautifulSoup(res.text, 'lxml')
for items in soup.select(".content [id^='single-id-']"):
    title = items.select_one("h4 a").text
    price = items.select_one(".price").text
    print(title,price)

相关问题 更多 >