gpt4 book ai didi

python - 使用 python、beautifulsoup 和 mechanize 选择下拉菜单

转载 作者:太空宇宙 更新时间:2023-11-03 16:02:02 29 4
gpt4 key购买 nike

我正在尝试从 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))

最佳答案

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

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

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

我可以想到两个选择:

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

关于python - 使用 python、beautifulsoup 和 mechanize 选择下拉菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40244829/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com