Python爬虫检查javascript行是否存在,如果存在,则解析i

2024-04-23 12:04:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我在python2.7中使用lxml.html文件我需要做以下事情。。。你知道吗

1)找出这一行是否在页面上。我其实是想看看父母是否存在。不是每页都有。你知道吗

DetailPage.StateController.setState('parent_asin', 'B0000DB87U');

2)如果存在,如何获取b000db87u?当父项在页面中时,每个页面的情况都会改变。这些都是javascript的,我使用lxml来获取和解析html。需要另一种JS方法。你知道吗


Tags: 文件html情况页面事情lxmlparent每页
1条回答
网友
1楼 · 发布于 2024-04-23 12:04:43

您可以使用lxml提取<script>标记的所有内容,然后用regex解析它们。你知道吗

未经测试的示例:

doc = lxml.html.parse(url)
scripts = doc.xpath('//script')
for script in scripts:
    match = re.findall(r"DetailPage\.StateController\.setState\('parent_asin', '(.*)'\);", script.text)
    if match:
        print match[0]

相关问题 更多 >