【问题标题】:Selecting dropdown with python, beautifulsoup and mechanize使用 python、beautifulsoup 和 mechanize 选择下拉菜单
【发布时间】:2017-03-07 18:57:16
【问题描述】:

我正在尝试从似乎是 ajax 网页的内容中抓取数据。数据每秒自动刷新一次。

http://daytonama.clubspeedtiming.com/sp_center/livescore.aspx

如果我选择了正确的下拉菜单,或者页面是否正在更改为我需要抓取的数据,我似乎无法解决。

谢谢

!/usr/bin/env python
import mechanize
from bs4 import BeautifulSoup
import re
import urllib2
#import html2text
import time

# Set credentials
venue = "sp" # Manchester (ma), Milton Keynes (mk), Sandown Park (sp), Tamworth (ta)
track = "3" # Manchester (3), Milton Keynes (1)

# Open new browser
br = mechanize.Browser()

# Target live timing page
resp = br.open("http://daytona"+ venue +".clubspeedtiming.com/sp_center/livescore.aspx")
html = resp.read()

# Grab live data table
soup = BeautifulSoup(html, "html5lib")

# Select track layout
select_node = soup.findAll('select', attrs={'name': 'ddlTrack'})

if select_node:
    for option in select_node[0].findAll('option'):
        print ''
        #print option.text

br.select_form( name = 'form1' )
br.form['ddlTrack'] = [track]

grid = soup.find("div", { "id" : "grid" })
print ''.join(map(str, grid.contents))

【问题讨论】:

    标签: python beautifulsoup mechanize


    【解决方案1】:

    通常ajax调用由异步请求触发,目标网页运行JS

    据我所知 mechanize.Browser 不是真正的浏览器,它不能执行和理解 javascript,它不能发送异步请求。

    在我看来,这就是您实际尝试输入到 BS4 的页面没有真正加载的原因,这就是您无法选择的原因。

    我能想到两个选择:

    1. 使用seleniumphantomJS(无头)作为浏览器。
    2. 分析网络并尝试找出网页正在执行的请求,然后仅模拟 ajax 请求,而不是尝试加载整个页面

    【讨论】:

    • 这非常有意义,因为定位在表格周围的 div 甚至不会显示空表格......显然因为它还没有被 JS 加载。谢谢!我至少现在有了另一种攻击计划!
    猜你喜欢
    • 1970-01-01
    • 2023-03-05
    • 2023-03-25
    • 2022-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    相关资源
    最近更新 更多