Python 如何创建嵌套的div和ol类

Python 如何创建嵌套的div和ol类,python,html,web-scraping,twitter,beautifulsoup,Python,Html,Web Scraping,Twitter,Beautifulsoup,我在努力刮 我想从“照片流容器”下载一些照片,但没有成功。下面是我目前正在使用的代码块 查找以“自适应”开头的所有跨类 例如,类将是“AdaptiveStreamGridImage grid tweet有卡有内容启用清除第一行hoverZoomLink” 有什么建议吗 d=requests.get('https://twitter.com/search?f=images&;垂直=新闻&;q=伊朗')。文本 soup=BeautifulSoup(d,'html.parser') spa

我在努力刮

我想从“照片流容器”下载一些照片,但没有成功。下面是我目前正在使用的代码块

查找以“自适应”开头的所有跨类 例如,类将是“AdaptiveStreamGridImage grid tweet有卡有内容启用清除第一行hoverZoomLink”

有什么建议吗

d=requests.get('https://twitter.com/search?f=images&;垂直=新闻&;q=伊朗')。文本
soup=BeautifulSoup(d,'html.parser')
span=soup.findAll(“span”,{“class”:lambda x:x和x.startswith('Adaptive')})
打印(跨距)
打印“跨距”时,我收到一个空列表

[]

您想要的内容很可能是通过JS脚本的强大功能隐藏起来的。我们的
请求
库不需要为那些JS脚本操心,它可以获取在浏览器的无JS模式下对您可见的内容。 这个问题可以在图书馆的帮助下解决。它允许你加载你的网页和它的内容,就像你使用的任何其他浏览器一样。 因此,使用Selenium有一些变通方法:

from selenium import webdriver
#Initiate your browser
browser = webdriver.Firefox() 
#It's Firefox in my case, you can have Chrome or Safari or Opera, depending upon the webdriver you have installed in your system
url = 'https://twitter.com/search?f=images&vertical=news&q=Iran'
#Fetch the URL in the 'browser'
browser.get(url)
#Get the page source of the browser
soup = BeautifulSoup(browser.page_source, 'html.parser')
#This page source is pretty similar to the one you see in your inspect element
browser.close() #'browser' has finished it's work, so 'close()' it
#Now apply whatever function you wish to on the webpage
spans = soup.findAll("span", {"class": lambda x: x and x.startswith('Adaptive')})
print(spans)

您想要的内容很可能是通过JS脚本的强大功能隐藏起来的。我们的
请求
库不需要为那些JS脚本操心,它可以获取在浏览器的无JS模式下对您可见的内容。 这个问题可以在图书馆的帮助下解决。它允许你加载你的网页和它的内容,就像你使用的任何其他浏览器一样。 因此,使用Selenium有一些变通方法:

from selenium import webdriver
#Initiate your browser
browser = webdriver.Firefox() 
#It's Firefox in my case, you can have Chrome or Safari or Opera, depending upon the webdriver you have installed in your system
url = 'https://twitter.com/search?f=images&vertical=news&q=Iran'
#Fetch the URL in the 'browser'
browser.get(url)
#Get the page source of the browser
soup = BeautifulSoup(browser.page_source, 'html.parser')
#This page source is pretty similar to the one you see in your inspect element
browser.close() #'browser' has finished it's work, so 'close()' it
#Now apply whatever function you wish to on the webpage
spans = soup.findAll("span", {"class": lambda x: x and x.startswith('Adaptive')})
print(spans)

您是否检查了页面源代码中是否存在要查找的元素?我正在查找包含单词“Adaptive”的span类,该单词是在检查页面时找到的。我建议使用
soup。选择('span[class^=Adaptive]')
,使用css选择器更酷我希望你知道禁止网络抓取的Twitter服务条款?(第4节“使用服务”)-可能导致您的IP地址被禁止。为什么不使用API?您是否检查了页面源代码中是否存在要查找的元素?我正在查找包含单词“Adaptive”的span类,该单词是在检查页面时发现的。我建议使用
soup。选择('span[class^=Adaptive]')
,使用css选择器更酷我希望你知道禁止网络抓取的Twitter服务条款?(第4节“使用服务”)-可能导致您的IP地址被禁止。为什么不使用API呢?