python - HTML 抓取具有重复 div 类名的网站

标签 python html parsing html-parsing html-parser

我目前正在研究 HTML 抓取 baka-update。 但是,Div Class 的名称是重复的。

由于我的目标是 csv 或 json,因此我想使用 [sCat] 中的信息作为列名称,使用 [sContent] 中的信息进行存储...... 他们有办法利用这种网站吗?

谢谢

示例 https://www.mangaupdates.com/series.html?id=75363

图片1 enter image description here 图2 enter image description here

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

enter image description here

最佳答案

我尝试编辑您的代码并得到以下输出。也许会有帮助。


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)


我得到以下输出

enter image description here

希望有帮助

关于python - HTML 抓取具有重复 div 类名的网站,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58851173/

相关文章:

python - 如何使用 Python Requests 登录这个特定的网站?

python - kwarg-splatting numpy 数组

python google oauth 仅限制域用户

c++ - 使用 C++11 正则表达式捕获上下文无关语法文件的内容

java - 识别数字数据的自然语言处理

python - fftpack 中缺少 scipy 函数

javascript - Bootstrap Modal 在页面加载时触发,每个用户一次

html - 选择不分组到其他输入的输入

html - 使滚动粘性元素的优先级高于更改元素位置

json - 将 JSON.parse() 位置转换为行号和列