无法从网页中删除类别标题

时间:2017-12-17 19:55:57

标签: python python-3.x web-scraping web-crawler

我已经在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>

2 个答案:

答案 0 :(得分:2)

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)

试试这个

  

用户代理是不够的,因为标头是最重要的部分   如果您错过任何标题,那么服务器会将您视为机器人。

答案 1 :(得分:-1)

使用"html.parser"代替"lxml"

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