我在Python 3.9中执行网络抓取脚本。
我想从该网站收集一些信息:https : //www.matchendirect.fr/。这个网站是法文,但我认为尝试帮助我不是一个真正的问题。
我需要的信息是鼠标在“ Pronostics des internautes”部分中显示的数组。HTML代码以开头:<table class="table table-bordered MEDtpro">
我已经重新创建了cookie,并在浏览器上模拟了我的连接,方法是按照本文的答案进行操作,该方法如何使用urllib发送cookie,但是没有用。
这是我的代码:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import requests
cookies = {
'PHPSESSID': 'a2q4evve875s1ibamiqmc93ru6',
'c_compte_cle': '76598fbd4fe763e768dc79275c02e11f',
'c_compte_id':'311084',
'c_compte_pseudo':'foobar',
'c_compte_url_image':'%2Fimage%2Fcommun%2Fmembre-med-t16.png',
'c_coucours_promo':'3'
}
headers = {'User-Agent': 'Mozilla/5.0'}
link = "https://www.matchendirect.fr/live-score/caen-toulouse.html"
response = requests.get(link, cookies=cookies, headers=headers)
webpage = response.text
print("Success!") if webpage.find('<table class="table table-bordered MEDtpro">')>-1 else print("Failed!")
有人可以帮我解决这个问题吗?
这个帐户是一个垃圾帐户,适合想要测试的用户。
您要查找的内容由AJAX
请求更新。您可以通过将请求发送到AJAX
URL来查找数据。
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import requests
from bs4 import BeautifulSoup
link = "https://www.matchendirect.fr/live-score/caen-toulouse.html"
response = requests.get(link)
soup = BeautifulSoup(response.content, "html.parser")
f_id_match = soup.find("input", {"name": "f_pronostic_id_match"})["value"]
data_response = requests.get("https://www.matchendirect.fr/cgi/ajax/liste_pronostic.php?f_id_match={}&f_id_grille=".format(f_id_match))
webpage = data_response.text
print("Success!") if webpage.find('<table class="table table-bordered MEDtpro">')>-1 else print("Failed!")
您需要f_id_match
从页面中查找,然后向该AJAX
网址发送新请求以查找所需内容。
不要设置Cookie来模仿浏览器,请使用
requests.Session()
来创建类似浏览器的会话,然后尝试在网址之间导航
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句