python - 用 Beautiful Soup 抓取时两页之间的差异
问题描述
我从 Python 和 Beautiful Soup 开始,我在 JSON 文件中抓取 Google PlayStore 和应用程序元数据。这是我的代码:
def createjson(app_link):
url = 'https://play.google.com/store/apps/details?id=' + app_link
response = get(url)
html_soup = BeautifulSoup(response.text, 'html.parser')
bs = BeautifulSoup(response.text,"lxml")
result = [e.text for e in bs.find_all("div",{"class":"hAyfc"})]
apptype = [e.text for e in bs.find_all("div",{"class":"hrTbp R8zArc"})]
data = {}
data['appdata'] = []
data['appdata'].append({
'name': html_soup.find(class_="AHFaub").text,
'updated': result[1][7:],
'apkSize': result[2][4:],
'offeredBy': result[9][10:],
'currentVersion': result[4][15:]
})
jsonfile = "allappsdata.json" #Get all the appS infos in one JSON
with open(jsonfile, 'a+') as outfile:
json.dump(data, outfile)
我的“结果”变量在特定应用页面中查找字符串,问题是 Google 正在更改两个不同页面之间的顺序。有时 result[1] 是应用程序名称,有时是 result[2];我需要的其他元数据('updated'、'apkSize'等)也有同样的问题我该如何处理这些更改。可以用其他方式刮吗?谢谢
解决方案
问题是python循环没有排序,将其保存为dict而不是列表。改变你result = [e....]
的
result = {}
details = bs.find_all("div",{"class":"hAyfc"})
for item in details:
label = item.findChild('div', {'class' : 'BgcNfc'})
value = item.findChild('span', {'class' : 'htlgb'})
result[label.text] = value.text
也data['appdata']...
与
data['appdata'].append({
'name': html_soup.find(class_="AHFaub").text,
'updated': result['Updated'],
'apkSize': result['Size'],
'offeredBy': result['Offered By'],
'currentVersion': result['Current Version']
推荐阅读
- c# - 为什么 EF 核心一对一关系没有按预期工作?
- python - 为什么通过 python 请求调用 post rest api 时出现 500 内部服务器错误?
- python - 如何在 Jupyter Notebook 中加载 CSV 文件?
- mysql - 用于 wp_options 的 MySQL 自动瞬态删除
- python - 模板中的Django使用if标签将一个变量与一个数字进行比较总是给出假
- sql - 如何对此进行正确的 SQL 查询:
- html - 如何找到横幅文本的 XPath?
- typescript - 在泛型函数中使用 keyof 正确进行类型推断
- reactjs - Typescript 和 Redux - 使用 Visual Studio 2017 的默认应用程序布局时出错
- c# - 使用 X 或创建按钮关闭表单