Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 试图刮取图像,我得到了空输出_Python_Python 3.x_Web Scraping_Beautifulsoup_Lxml - Fatal编程技术网

Python 试图刮取图像,我得到了空输出

Python 试图刮取图像,我得到了空输出,python,python-3.x,web-scraping,beautifulsoup,lxml,Python,Python 3.x,Web Scraping,Beautifulsoup,Lxml,我正在尝试刮Twitter帐户图像,我尝试了多种方法,输出结果总是给我空列表 我的代码: import requests from bs4 import BeautifulSoup url = requests.get('https://twitter.com/jack/photo') soup = BeautifulSoup(url.text, 'lxml') image = soup.find_all('img') print(image) 输出: [] 那是我项目的一部分。。我尝

我正在尝试刮Twitter帐户图像,我尝试了多种方法,输出结果总是给我空列表

我的代码:

import requests
from bs4 import BeautifulSoup


url = requests.get('https://twitter.com/jack/photo')
soup = BeautifulSoup(url.text, 'lxml')
image = soup.find_all('img')

print(image)
输出:

[]
那是我项目的一部分。。我尝试了lxml,并按类查找,但仍然一无所获,也许我遗漏了一些东西,但我不知道它是什么。 如果有人能帮我,我将不胜感激


提前感谢

我可以看到页面中使用了一些React。如果你打开页面并检查元素,你会看到,只要你点击照片放大,一个新的div就会出现,就像从稀薄的空气中。这意味着它是由react创建的


为了解决这个问题,您需要使用在
虚拟浏览器中打开页面,让JavaScript发挥它的魔力,然后查找
img
标记。

您正在尝试为JavaScript twitter搜索路径。如果您检查页面的响应,您将看到以下snippit

    <form action="https://mobile.twitter.com/i/nojs_router?path=%2Fjack%2Fphoto" method="POST" style="background-color: #fff; position: fixed; top: 0; left: 0; right: 0; bottom: 0; z-index: 9999;">
  <div style="font-size: 18px; font-family: Helvetica,sans-serif; line-height: 24px; margin: 10%; width: 80%;">
    <p>We've detected that JavaScript is disabled in your browser. Would you like to proceed to legacy Twitter?</p>
    <p style="margin: 20px 0;">
      <button type="submit" style="background-color: #1da1f2; border-radius: 100px; border: none; box-shadow: none; color: #fff; cursor: pointer; font-size: 14px; font-weight: bold; line-height: 20px; padding: 6px 16px;">Yes</button>
    </p>
  </div>
</form>

注意:Twitter经常更改其布局,因此这可能不会持续很久。

Twitter正在使用某种前端框架。我检查过,当你查看页面的源代码时,根本没有img标签。尝试使用seleniumYes,我认为它可以与selenium一起使用,但我希望它与BeautifulSoup或lxml或scrapy一起使用,但不是selenium,因为我想将项目主办给HeroKu,而selenium与HeroKu不匹配。是的,我尝试了selenium,效果很好,但我希望它与BeautifulSoup或lxml或任何其他刮削库一起使用,我不想使用selenium,因为它不能很好地与HeroKu配合使用(我想最终主持我的项目),不幸的是,除非您能够找到网页从何处以及如何提取图像(例如,如果它有某种形式的API,它可以点击以获取图像),您将需要JS为您呈现页面,这将意味着使用类似Selenium的东西。可能还有其他一些方法,正如其他响应所表达的那样。非常感谢您,我更改了链接并编写了
image=soup.find_all('div',{“class:css-9pa8cd”})print(image)
,但仍然是空列表,老实说,我不知道如何使用css选择器以及如何处理BeautifulSoup genrally,你能给我看看你的代码或者解释一下我是如何得到它的吗?
import requests
from bs4 import BeautifulSoup


response = requests.get('https://mobile.twitter.com/jack')

soup = BeautifulSoup(response.text, 'lxml')

avatars = soup.findAll("td", {"class": "avatar"})

print(avatars[0].findAll('img')[0].get('src'))