簡體   English   中英

無法從網頁上抓取類別標題

[英]Can't scrape category titles from a webpage

我用python編寫了一個scraper,以從網頁中獲取不同的類別名稱,但它無法從該網頁中獲取任何內容。 我很困惑,無法找出我要去哪里。 任何幫助將不勝感激。

這是網頁的鏈接: URL

到目前為止,這是我嘗試過的:

from bs4 import BeautifulSoup
import requests

res = requests.get("replace_with_above_url",headers={"User-Agent":"Mozilla/5.0"})
soup = BeautifulSoup(res.text,"lxml")
for items in soup.select('.slide_container .h3.standardTitle'):
    print(items.text)

我在其中使用這樣一個類別名稱的元素:

<div class="slide_container">
    <a href="/offers/furniture/" tabindex="0">
        <picture style="float: left; width: 100%;"><img style="width:100%" src="/_m4/9/8/1513184943_4413.jpg" data-w="270"></picture>
        <div class="floated-details inverted" style="height: 69px;">
            <div class="h3 margin-top-sm margin-bottom-sm standardTitle">
                Furniture Offers                         #This is the name I'm after
            </div>
            <p class="carouselDesc">
            </p>
        </div>
    </a>
</div>
from bs4 import BeautifulSoup
import requests

headers = {
    'accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
'accept-encoding':'gzip, deflate, br',
'accept-language':'en-US,en;q=0.9',
'cache-control':'max-age=0',
'referer':'https://www.therange.co.uk/',
'upgrade-insecure-requests':'1',
'user-agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36',
}
res = requests.get("https://www.therange.co.uk/",headers=headers)
soup = BeautifulSoup(res.text,'html.parser')
for items in soup.select('.slide_container .h3.standardTitle'):
    print(items.text)

嘗試這個

用戶代理是不夠的,因為標頭是報廢的最重要部分。如果您錯過任何標頭,則服務器會將您視為機器人。

使用"html.parser"而不是"lxml"

soup = BeautifulSoup(res.text,"html.parser")

暫無
暫無

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

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