保存麻烦的网页并导入回Python



我试图从各种页面中提取一些信息,但有点困难。这显示了我的挑战:

import requests
from lxml import html
url = "https://www.soccer24.com/match/C4RB2hO0/#match-summary"
response = requests.get(url)
print(response.content)

如果您将输出复制到记事本中,则在输出的任何位置都找不到值"9.20"(网页右下角的A队赔率(。然而,如果你打开网页,做一个"另存为",然后像这样将其导入Python,你可以找到并提取9.20值:

with open(r'HUL 1-7 TOT _ Hull - Tottenham _ Match Summary.html', "r") as f:
    page = f.read()
tree = html.fromstring(page)
output = tree.xpath('//*[@id="default-odds"]/tbody/tr/td[2]/span/span[2]/span/text()')  #the xpath for the TeamA odds or the 9.20 value
output # ['9.20']

我不知道为什么这种变通方法有效,但这是我无法理解的。所以我想做的是将一个网页保存到我的本地驱动器中,并用Python打开它,如上所述,然后从那里继续。但是如何在Python中复制"另存为"?这不起作用:

import urllib.request
response = urllib.request.urlopen(url)
webContent = response.read().decode('utf-8')
f = open('HUL 1-7 TOT _ Hull - Tottenham _ Match Summary.html', 'w')
f.write(webContent)
f.flush()
f.close()

它给了我一个网页,但它只是原始页面的一小部分。。。?

正如@Pedro Lobito所说。页面内容由javascript生成。因此,您需要一个可以运行JavaScript的模块。我将选择requests_htmlselenium

请求_ html

from requests_html import HTMLSession
url = "https://www.soccer24.com/match/C4RB2hO0/#match-summary"
session = HTMLSession()
response = session.get(url)
response.html.render()
result = response.html.xpath('//*[@id="default-odds"]/tbody/tr/td[2]/span/span[2]/span/text()')
print(result)
#['9.20']

from selenium import webdriver
from lxml import html
url = "https://www.soccer24.com/match/C4RB2hO0/#match-summary"
dr = webdriver.Chrome()
try:
dr.get(url)
tree = html.fromstring(dr.page_source)
''' use it when browser closes before loading succeeds
# https://selenium-python.readthedocs.io/waits.html
WebDriverWait(dr, 10).until(
EC.presence_of_element_located((By.ID, "myDynamicElement"))
)
'''
output = tree.xpath('//*[@id="default-odds"]/tbody/tr/td[2]/span/span[2]/span/text()')  #the xpath for the TeamA odds or the 9.20 value
print(output)
except Exception as e:
raise e
finally:
dr.close()
#['9.20']

最新更新