使用Python beautifulSoup抓取特定的dd项目

匿名13

我正在尝试使用Python从网站中提取特定的'dd'元素

headers = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) '\
           'AppleWebKit/537.36 (KHTML, like Gecko) '\
           'Chrome/75.0.3770.80 Safari/537.36'}

url = "https://www.ranger5g.com/forum/threads/pre-collision-assist.3239"
page = requests.get(url, headers=headers)
soup = BeautifulSoup(page.text, 'html.parser')


vehicle=[]

for i in soup.findAll("div", class_="message-userExtras"):
    for item in soup.find_all("dd")[::-1]:
        vehicle.append(item.get_text())
print(vehicle)

我正在尝试仅从网址中提取车辆列表,我的输出应如下所示

2019 Ford Ranger XLT FX4
2019 Ford Ranger Lariat FX4, 1973 Mercury Capri
Tahoe/Tundra/Fusion
2019 Ford Ranger Lariat - Saber; 2014 GMC Terrain

但是我的结果不是我所期望的

昆杜克

使用正则表达式re并dt用文本搜索标签,Vehicle然后找到下一个dd标签。

import re
from bs4 import BeautifulSoup
import requests
headers = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) '\
           'AppleWebKit/537.36 (KHTML, like Gecko) '\
           'Chrome/75.0.3770.80 Safari/537.36'}

url = "https://www.ranger5g.com/forum/threads/pre-collision-assist.3239"
page = requests.get(url, headers=headers)
soup = BeautifulSoup(page.text, 'html.parser')

for item in soup.find_all("div",class_='message-userExtras'):
    print(item.find('dt',text=re.compile("Vehicle")).find_next('dd').text.strip())

输出:

2019 Ford Ranger XLT FX4
2019 Ford Ranger Lariat FX4, 1973 Mercury Capri
Tahoe/Tundra/Fusion
2019 Ford Ranger Lariat - Saber; 2014 GMC Terrain
2019 Ford Ranger Lariat FX4, 1973 Mercury Capri
2019 Ranger Lariat - 2019 Honda CRV Touring
2019 Ford Ranger XLT FX4
2019 Ford Ranger Lariat FX4, 1973 Mercury Capri
2019 Ranger Lariat SuperCab
2019 Ranger Lariat
Ranger Lariat
2019 Ford Ranger Lariat
Ranger Lariat
Ranger Lariat
2019 Ranger XLT 301A SuperCrew 4X4 2015 Ecoboost Mustang 50 Year Appereance Package convertible

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用BeautifulSoup抓取特定网站

如何在使用 BeautifulSoup 进行网页抓取时访问 <li> 中的特定项目?

想抓取每个特定元素,但使用python beautifulsoup却“无”

使用 Python 抓取 HTML 中的特定元素:BeautifulSoup4

使用 beautifulsoup 抓取代碼的特定部分

使用BeautifulSoup从网页中抓取特定链接

如何使用 BeautifulSoup 抓取特定数据

无法使用 BeautifulSoup 抓取特定表格

使用 BeautifulSoup 抓取:从 HTML 页面抓取表格中的特定列

使用Python抓取Json数据中的特定项目

使用BeautifulSoup Python抓取网页

使用 python 抓取网站 - BeautifulSoup

使用beautifulsoup抓取HTML网站ID的特定部分

使用 BeautifulSoup 从 html 中抓取特定数据

使用BeautifulSoup和Selenium抓取特定的html标签

如何使用BeautifulSoup抓取基于特定链接的文本?

使用BeautifulSoup从网页的特定部分抓取所有图像

使用 BeautifulSoup 抓取特定国家/地区的产品页面

使用 BeautifulSoup 抓取 CSS 类中的特定元素

使用beautifulsoup python从页面中抓取特定元素时遇到问题

如何使用BeautifulSoup,Requests和Python从HTML的特定表中抓取数据?

使用 Python 抓取特定页面

如何使用 Python BeautifulSoup 抓取 ID

使用python和BeautifulSoup进行网络抓取

使用Python,BeautifulSoup进行Web抓取

使用 BeautifulSoup 在 python 中抓取多个页面

使用 Python 和 BeautifulSoup 进行问题抓取

如何使用BeautifulSoup和Python抓取页面?

使用 Python BeautifulSoup 进行网页抓取