我試圖從使用Python(BeautifulSoup)的少數網站收集數據。但是,有時很難獲得搜索結果,例如:使用Python請求訪問動態生成的網站內容
import requests
from bs4 import BeautifulSoup
url1 = 'https://auto.ria.com/legkovie/city/vinnica/?page=1'
url2= 'https://auto.ria.com/search/?top=11&category_id=1&state[0]=1'
def get_value(url):
r = requests.get(url, headers = {'Accept-Encoding' : 'deflate'})
print("Response Time: {}".format(r.elapsed.total_seconds()))
soup = BeautifulSoup(r.text, 'lxml')
data = soup.find('span', attrs = {'id' : 'resultsCount'}).find('strong')
print('{} \n'.format(data))
get_value(url1)
get_value(url2)
輸出是:
Response Time: 5.4943
<strong class="count">5 310</strong>
Response Time: 0.174867
<strong class="count">0</strong>
雖然URL2在瀏覽器中顯示的號碼的情況下,爲338 我想,搜索結果在某些JSON中找到,但如何使用請求訪問它?
「我想可以在某些json中找到搜索結果」:您是否試圖找到它? –