从meta标签中提取数据beautifulsoup返回null

时间:2018-02-17 20:30:03

标签: python html beautifulsoup meta

我试图从这个网站上刮掉一辆车的里程数 https://cazana.com/uk/car/RA51GZJ

我想要的数据是里程数(128,375英里) 当我试图刮这个页面时,我得不到任何回报 我原本试图在没有运气的情况下浏览页面正文

url = "https://cazana.com/uk/car/RA51GZJ"
page2 = requests.get(url)
soup2 = BeautifulSoup(page2.content, 'html.parser')
result = soup2.findAll('meta', attrs={'name': 'description'})

print (result)

返回[]

这是html文件

 <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="RA51GZJ - 2001 NISSAN ALMERA. Colour silver, 128,375 miles, 3 previous owners. Registered in Reading. Tax, MOT &amp; Vehicle history check available.">

谢谢

1 个答案:

答案 0 :(得分:3)

您的请求未成功,这就是您找不到正确标记的原因。返回的内容是错误页面。
您可以通过更改User-Agent标题来绕过此错误对于浏览器:

import requests
from bs4 import BeautifulSoup

url = 'https://cazana.com/uk/car/RA51GZJ'

headers = {
    'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64)'
    'AppleWebKit/537.36 (KHTML, like Gecko)'
    'Chrome/64.0.3282.167 Safari/537.36'
}

result = requests.get(url, headers=headers)
soup = BeautifulSoup(result.content, 'html.parser')
match = soup.find('meta', name='description')

if match:
    print(match.attrs['content'])
else:
    print('Request unsuccessful')

请注意,一次过多的请求也会触发不成功的请求。