使用beautifulSoup时html内容会发生变化

问题描述 投票:0回答:1

我试图从一个html块中提取src的属性值,html块是:

<img class="product-image first-image" src="https://cache.net-a-porter.com/images/products/1083507/1083507_in_pp.jpg">

我的代码是:

import requests
import json
from bs4 import BeautifulSoup
import re
headers = {'User-agent': 'Mozilla/5.0'}
url = 'https://www.net-a-porter.com/us/en/product/1083507/maje/layered-plaid-twill-and-stretch-cotton-jersey-top'
page = requests.get(url, headers=headers)
soup = BeautifulSoup(page.content, 'html.parser')
if url.find('net-a-porter')!=-1 :
  i = soup.find_all('img', class_="product-image first-image")[0]["src"]
  print i

结果我得到:

//cache.net-a-porter.com/images/products/1083507/1083507_in_xs.jpg

但我想得到原始html中的内容,应该是:

https://cache.net-aporter.com/images/products/1083507/1083507_in_pp.jpg

我的结果不同于原来的src值,http:is消失了,而1083507_in_pp改为1083507_in_xs。我不知道为什么会这样,有谁知道如何解决这个问题?谢谢!

python html beautifulsoup python-requests web-crawler
1个回答
0
投票

你很接近,但是,你需要从内置的"src"密钥访问attrs密钥:

if url.find('net-a-porter')!=-1 :
  i = soup.find_all('img', class_="product-image first-image")[0]
  print i['src']
© www.soinside.com 2019 - 2024. All rights reserved.