簡體   English   中英

python請求無法獲取完整的數據

[英]python request get not fetching the complete data

我想從https://www.pastemagazine.com/blogs/lists/2009/11/the-best-albums-of-the-decade.html刮取十年(2000-2009年)的50張最佳專輯? a = 1

我在python中使用以下代碼:

from requests import get 
url = 'https://www.pastemagazine.com/blogs/lists/2009/11/the-best-albums-of-the-decade.html?a=2'
response = get(url) 
print(response.text)

當我查看響應時,輸出中缺少所有50張最佳專輯的信息。 當我查看頁面源代碼時,確實在<div class="grid-x article-wrapper">下看到了此信息。 為了抓取這部分網頁,我需要做什么?

您需要定義標題,使其更像真正的瀏覽器。 以下應該工作。

import requests
from bs4 import BeautifulSoup

url = 'https://www.pastemagazine.com/blogs/lists/2009/11/the-best-albums-of-the-decade.html?a=2'

res = requests.get(url,headers={"User-Agent":"Mozilla/5.0"}) 
soup = BeautifulSoup(res.text,"lxml")
for item in soup.select("b.big > b"):
    print(item.text)

輸出如下:

50. Björk: Vespertine [Elektra] 2001
49. Libertines: Up The Bracket [Rough Trade] (2002)
48. Loretta Lynn: Van Lear Rose [Interscope] (2004)
47. Arctic Monkeys: Whatever People Say I Am, That’s What I’m Not [Domino] (2006)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM