从返回 null 的元标记 beautifulsoup 中提取数据
Extracting data from a meta tag beautifulsoup returning null
我想从这个网站上抓取汽车的里程数
https://cazana.com/uk/car/RA51GZJ
我要的数据是里程数(128375英里)
当我尝试抓取此页面时,我什么也没得到
我最初试图在没有运气的情况下对页面主体进行转义
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)
Returns[]
这是 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 & Vehicle history check available.">
谢谢
您的请求不成功,这就是您找不到正确标签的原因。
返回的内容是一个错误页面。
您可以通过更改您的 User-Agent
header 到浏览器的:
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')
请注意,一次请求过多也会触发不成功的请求。
我想从这个网站上抓取汽车的里程数 https://cazana.com/uk/car/RA51GZJ
我要的数据是里程数(128375英里) 当我尝试抓取此页面时,我什么也没得到 我最初试图在没有运气的情况下对页面主体进行转义
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)
Returns[]
这是 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 & Vehicle history check available.">
谢谢
您的请求不成功,这就是您找不到正确标签的原因。
返回的内容是一个错误页面。
您可以通过更改您的 User-Agent
header 到浏览器的:
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')
请注意,一次请求过多也会触发不成功的请求。