python - Python 3 bs4 爬行链接问题和字典列表问题
问题描述
好久没爬网了,被网站屏蔽了。。。
如果我用 chrome 检查页面,我会看到源代码中的所有链接,但是当使用 bs4 和 python 3 时,汤将不包含任何链接!
由于我是爬行新手,所以我希望有人向我解释一下:)
是因为代理吗?爬取这个页面是绝对不可能的吗?
import requests
from bs4 import BeautifulSoup
import re
url="https://www.pointdevente.parionssport.fdj.fr/grilles/resultats"
request_headers={'User-Agent': "(Mozilla/5.0 (Windows; U; Windows NT 6.0;en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6" }
r = requests.get(url,headers=request_headers)
req_txt=r.text
if r.status_code==200:
soup = BeautifulSoup(req_txt, u'html5lib')
print(soup.prettify())
links_pak=[url + u'/' + node.get(u'href') for node in soup.find_all(u'a')] #if node.get(u'href').endswith(ext)]
print("links : ",links_pak)
else:
print(r.status_code)
编辑:经过一些研究,我发现这是由于 JS 前端代码,所以我查看了网络行为并发现了一些我应该能够用于我需要的 API 调用。
所以这是我的字典问题列表,所以我有以下代码:
import requests
from bs4 import BeautifulSoup
import re
url="https://www.pointdevente.parionssport.fdj.fr/api/loto-foot/list"
r = requests.get(url)
req_txt=r.text
if r.status_code==200:
print(r.json())
list_all=r.json()
else:
print(r.status_code)
我查看了很多文档,并且我了解如何过滤键值上的字典或查看字典列表中是否有值,但我想要检查我的字典列表(例如 list_all)是否有键:字典中的值元组,如果是这样,则仅检索此字典的数据!
在我的示例中,如果您运行代码片段,您将在 dicts 中看到一个 'sportId' 键,我只想在 'sportId:100' 为 true 的情况下获取 dicts ......我对 dict 理解有点困惑。 ...
谢谢您的帮助 !
解决方案
好的,经过多次列表理解尝试后,我发现了如何做......
所以这里的代码片段工作:
import requests
from bs4 import BeautifulSoup
import re
url="https://www.pointdevente.parionssport.fdj.fr/api/loto-foot/list"
r = requests.get(url)
req_txt=r.text
if r.status_code==200:
list_all=r.json()
list_foot=[d for d in list_all if d['sportId']==100] #Filters foot results only
print(list_foot)
else:
print(r.status_code)
推荐阅读
- python - 在 Google Maps API 中使用 Location-bias 查找广场上的所有商店
- json - 在 React Native 上解析 Json 并从中撤回数据
- swift - swiftUI:在 onAppear 崩溃中更改 @State
- html - 我如何努力让台式机/笔记本电脑上的固定侧边栏菜单切换到手机/平板电脑上的汉堡菜单
- c - EXC_BAD_ACCESS 使用 fgets() 和 strtok() 时
- javascript - 为什么在使用 combineLatest 时不会在内部 observable 上调用运算符(点击、地图)?
- angular - 从角垫表中删除一列
- java - 尝试理解 Java 中的访问修饰符
- ruby-on-rails - Rails API + VueJS 服务器布局架构
- python - 如何构建 ARMA 或 ARIMA 模型?