python - 在网络抓取中寻找项目
问题描述
我正在寻找一种在亚马逊商店中刮取作者和价格的方法。(然后删除美元符号,在输出中保留 3.99。)
到目前为止,我已经尝试并获得了标题和评级,但不确定如何检索作者的姓名。
from bs4 import BeautifulSoup
import pandas as pd
import requests
url = ' '
html_content = requests.get(url).text
soup = BeautifulSoup(html_content, "html")
rate = []
for x in soup.select("li.zg-item-immersion"):
item = {}
item['name'] = x.select_one('a').get_text(strip=True)
item['rank'] = x.select_one('span span').get_text(strip=True)
rate.append(item)
rate
在上述输入之后的输出中,我得到:
只是想知道如何删除每个名称部分括号中的项目。
例如“就在她身后(Bree Taggert Book 4)”
可以“就在她身后”
解决方案
您可以使用此代码:
x.find("span", {"class": "p13n-sc-price"}).get_text().split('$')[1]
整个代码:
from bs4 import BeautifulSoup
import pandas as pd
import requests
url = 'https://www.amazon.com/Best-Sellers-Kindle-Store/zgbs/digital-text'
html_content = requests.get(url).text
soup = BeautifulSoup(html_content, "html")
rate = []
for x in soup.select("li.zg-item-immersion"):
item = {}
item['name'] = x.select_one('a').get_text(strip=True).split('(')[0].strip()
item['rank'] = x.select_one('span span').get_text(strip=True)
item['price'] = x.find("span", {"class": "p13n-sc-price"}).get_text().split('$')[1]
try:
item['author'] = x.find("a", {"class":"a-size-small a-link-child"}).text
except:
item['author'] = 'Not Found Author Name'
rate.append(item)
输出:
[{'name': 'Peril', 'rank': '#1', 'price': '14.99', 'author': 'Bob Woodward'},
{'name': 'Apples Never Fall',
'rank': '#2',
'price': '14.99',
'author': 'Liane Moriarty'},
...
]
推荐阅读
- makefile - 使用带有通配符目标的 nmake
- ios - 如何设置...在 mopub ios sdk 中初始化多个单元 id
- python-3.x - 熊猫数据框中的布尔索引
- java - AoC 第 8 天问题(Java)
- javascript - 如何在 html 中有一个开放日期选择器
- python - Python:使用 for 循环在 numpy 向量(包含随机 0,1,2 数字)中搜索序列 (1,1,2):
- r - Create new column with vector values based on groups from another character column
- c# - 在 MVC 网络核心中使用 Rotativa pdf 显示动态标题
- animation - 带有透明背景的 gif 动画
- python - 如何在急切执行模式下获取 keras 模型的可训练变量?