我目前正在研究 HTML 抓取 baka-update。 但是,Div Class 的名称是重复的。
由于我的目标是 csv 或 json,因此我想使用 [sCat] 中的信息作为列名称,使用 [sContent] 中的信息进行存储...... 他们有办法利用这种网站吗?
谢谢
示例 https://www.mangaupdates.com/series.html?id=75363
from lxml import html
import requests
page = requests.get('http://www.mangaupdates.com/series.html?id=153558?')
tree = html.fromstring(page.content)
#Get the name of the columns.... I hope
sCat = tree.xpath('//div[@class="sCat"]/text()')
#Get the actual data
sContent = tree.xpath('//div[@class="sContent"]/text()')
print('sCat: ', sCat)
print('sContent: ', sContent)
我尝试过,但找不到任何东西 @Jasper Nichol M Fabella
最佳答案
我尝试编辑您的代码并得到以下输出。也许会有帮助。
from lxml import html
import requests
page = requests.get('http://www.mangaupdates.com/series.html?id=153558?')
tree = html.fromstring(page.content)
# print(page.content)
#Get the name of the columns.... I hope
sCat = tree.xpath('//div[@class="sCat"]')
#Get the actual data
sContent = tree.xpath('//div[@class="sContent"]')
print('sCat: ', len(sCat))
print('sContent: ', len(sContent))
json_dict={}
for i in range(0,len(sCat)):
# print(''.join(i.itertext()))
sCat_text=(''.join(sCat[i].itertext()))
sContent_text=(''.join(sContent[i].itertext()))
json_dict[sCat_text]=sContent_text
print(json_dict)
我得到以下输出
希望有帮助
关于python - HTML 抓取具有重复 div 类名的网站,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58851173/