我正在嘗試從https://betsapi.com/獲取一些資料,特別是使用 python 從足球領域獲取我在代碼中看到鏈接是動態的,我的意思是幾周前它是https:// betsapi.com/cin/soccer現在是https://betsapi.com/cip/soccer。
查看代碼我想了解如何從這部分代碼中識別當前的足球鏈接。
<div >
<a href="/" >
All (70)
</a>
<a href="/cip/basketball" ></a>
<a href="/cip/soccer" ></a>
<a href="/cip/horse-racing" > </a>
<a href="/cip/greyhounds" ></a>
<a href="/cip/ice-hockey" ></a>
<a href="/cip/table-tennis" ></a>
<a href="/cip/volleyball" ></a>
<div >
<a href="#" data-toggle="dropdown" aria-expanded="true">More</a>
<div x-placement="bottom-end" style="position: absolute; transform: translate3d(-109px, 55px, 0px); top: 0px; left: 0px; will-change: transform;">
<a href="/cip/golf"></a>
<a href="/cip/tennis"></a>
<a href="/cip/baseball"></a>
<a href="/cip/esports"></a>
<a href="/cip/darts"></a>
<a href="/cip/handball"></a>
<a href="/cip/futsal"></a>ù
非常感謝
uj5u.com熱心網友回復:
我只會搜索卡片標簽項并查找'soccer'. 然后列印href以獲取鏈接:
import requests
from bs4 import BeautifulSoup
url = 'https://betsapi.com'
headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.141 Safari/537.36'}
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, 'html.parser')
cards = soup.find_all('a', {'class':'card-tabs-item'})
soccer = [x for x in cards if 'soccer' in x['href']][0]
link = url soccer['href']
輸出:
print(link)
https://betsapi.com/cip/soccer
uj5u.com熱心網友回復:
作為替代方案,您可以css selectors選擇并選擇<a>:
href應以soccer:link = url soup.select_one('a[href$="soccer"]')['href']或更具體:
link = url soup.select_one('a.card-tabs-item[href$="soccer"]')['href']href應包含soccer:link = url soup.select_one('a[href*="soccer"]')['href']或更具體:
link = url soup.select_one('a.card-tabs-item[href*="soccer"]')
例子
import requests
from bs4 import BeautifulSoup
url = 'https://betsapi.com'
headers = {'user-agent': 'Mozilla/5.0'}
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, 'html.parser')
link = url soup.select_one('a.card-tabs-item[href$="soccer"]')['href']
print(link)
輸出
https://betsapi.com/cip/soccer
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/455134.html
標籤:网页抓取
上一篇:傀儡師,帶回空白陣列
