【问题标题】:urlopen not getting all the data from web (python)urlopen 没有从网络获取所有数据(python)
【发布时间】:2019-07-25 22:15:16
【问题描述】:

我正在尝试从网站下载图片。我发现我找不到图片URL的问题是在代码的开头。

我有一个问题,即 urlopen 下载的 HTML 与我在浏览器中下载的 HTML 不同。

该网站是here。当我在浏览器中查看 HTML 时,我可以看到这部分:

HTML in browser

<a href="#" data-trigger="cmg-rotate-big">
            <img src="/image/product/eca412b9-9484-4046-8bee-8400fde1d5fe/?width=400" alt="" data-cm-index="0" style="width: 400px; height: 400px; margin-left: 0px; opacity: 1;">
            <img src="/image/product/014a128e-fa7b-4817-9d76-7bdf296de8de/?width=400" alt="" data-cm-index="1" style="width: 0px; height: 400px; margin-left: 200px; opacity: 0.5;">
          </a>

但是通过代码

text = urllib2.urlopen(url).read()
soup = BeautifulSoup(text, "html.parser")
print(soup)

只有相同的部分

<a data-trigger="cmg-rotate-big" href="#">
<img alt="" data-cm-index="0" src=""/>
<img alt="" data-cm-index="1" src=""/>
</a>

所以我可以提取图像的 SRC,因为它丢失了。请问问题出在哪里?

谢谢!

【问题讨论】:

  • 您需要一些东西来模拟填充这些 DOM 元素的 Javascript 代码。见this answer
  • 我会尝试 selenium,感谢这个想法 :)
  • Selenium 工作,谢谢。

标签: python html beautifulsoup urllib2


【解决方案1】:

src href 在那里。无需模拟javascript。

import requests
import bs4

url = 'https://ceskamincovna.cz/stribrna-mince-na-kolech---skoda-felicia-proof-1493-11549-d/'

response = requests.get(url) 

soup = bs4.BeautifulSoup(response.text , 'html.parser')
imgs = soup.find_all('img')
for img in imgs:
    if '/image/product/' in img['src']:
        print (img['src'])

输出:

/image/product/eca412b9-9484-4046-8bee-8400fde1d5fe/?width=250
/image/product/014a128e-fa7b-4817-9d76-7bdf296de8de/?width=250
/image/product/0ec5b392-0f8a-4013-a448-a1b82578c008/?width=250
/image/product/9bc26462-5f11-4994-be6e-fcde1d97c5f3/?width=250
/image/product/7da1f235-f322-4a57-b0ca-07964f0a7d37/?width=250
/image/product/bd781b17-8482-4a4f-80f3-5fa55b9bc4c1/?width=250
/image/product/f5d4ade9-cac0-4c15-a935-da125b408da1/?width=250
/image/product/f4d6fb41-af72-4510-a70c-0a9893656e93/?width=250
/image/product/6136afe7-7444-42cd-858b-af66ca4ca6de/?width=140
/image/product/a459eb25-dd12-446a-9517-341d128c9571/?width=140

如果你想要宽度 = 400:

import requests
import bs4

url = 'https://ceskamincovna.cz/stribrna-mince-na-kolech---skoda-felicia-proof-1493-11549-d/'

response = requests.get(url) 

soup = bs4.BeautifulSoup(response.text , 'html.parser')
imgs = soup.find_all('img')
for img in imgs:
    if '/image/product/' in img['src']:
        print (img['src'].split('?width=')[0] + '?width=400')

输出:

/image/product/eca412b9-9484-4046-8bee-8400fde1d5fe/?width=400
/image/product/014a128e-fa7b-4817-9d76-7bdf296de8de/?width=400
/image/product/0ec5b392-0f8a-4013-a448-a1b82578c008/?width=400
/image/product/9bc26462-5f11-4994-be6e-fcde1d97c5f3/?width=400
/image/product/7da1f235-f322-4a57-b0ca-07964f0a7d37/?width=400
/image/product/bd781b17-8482-4a4f-80f3-5fa55b9bc4c1/?width=400
/image/product/f5d4ade9-cac0-4c15-a935-da125b408da1/?width=400
/image/product/f4d6fb41-af72-4510-a70c-0a9893656e93/?width=400
/image/product/6136afe7-7444-42cd-858b-af66ca4ca6de/?width=400
/image/product/a459eb25-dd12-446a-9517-341d128c9571/?width=400

【讨论】:

  • 我需要一张以 "width=400" 结尾的图片 :)
  • 然后只操作字符串。它只是一个显示图像的参数。您可以将其更改为 width=400
  • 好主意,如果文件的其余部分相同,但我已经通过 selenium 解决了这个问题 :) 谢谢! :)
  • 是的,Selenium 也是一个可行的解决方案,因为它是动态创建的内容。
猜你喜欢
  • 1970-01-01
  • 2016-11-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-02
  • 1970-01-01
  • 1970-01-01
  • 2023-02-26
相关资源
最近更新 更多