使用 Beautiful Soup 问题抓取数据
我正在努力从该网站抓取宇航员的国家/地区: https://www.supercluster.com/astronauts?ascending=false&limit=72&list=true&sort=launch%20order。我正在使用 BeautifulSoup 来执行此任务,但遇到了一些问题。这是我的代码:
import requests
from bs4 import BeautifulSoup
import pandas as pd
data = []
url = 'https://www.supercluster.com/astronauts?ascending=false&limit=72&list=true&sort=launch%20order'
r = requests.get(url)
soup = BeautifulSoup(r.content,'html.parser')
tags = soup.find_all('div', class_ ='astronaut_index__content container--xl mxa f fr fw aifs pl15 pr15 pt0')
for item in tags:
name = item.select_one('bau astronaut_cell__title bold mr05')
country = item.select_one('mouseover__contents rel py05 px075 bau caps small ac').get_text(strip = True)
data.append([name,country])
df = pd.DataFrame(data)
df
df 返回一个空列表。不知道发生了什么事。当我将代码从 for 循环中取出时,它似乎找不到 select_one 函数。功能应该来自 bs4 - 不知道为什么它不起作用。另外,是否有我缺少的可重复的网络抓取模式?每次我尝试解决这类问题时,似乎都是不同的野兽。
任何帮助将不胜感激!谢谢你!
I am working on scraping the countries of astronauts from this website: https://www.supercluster.com/astronauts?ascending=false&limit=72&list=true&sort=launch%20order. I am using BeautifulSoup to perform this task, but I'm having some issues. Here is my code:
import requests
from bs4 import BeautifulSoup
import pandas as pd
data = []
url = 'https://www.supercluster.com/astronauts?ascending=false&limit=72&list=true&sort=launch%20order'
r = requests.get(url)
soup = BeautifulSoup(r.content,'html.parser')
tags = soup.find_all('div', class_ ='astronaut_index__content container--xl mxa f fr fw aifs pl15 pr15 pt0')
for item in tags:
name = item.select_one('bau astronaut_cell__title bold mr05')
country = item.select_one('mouseover__contents rel py05 px075 bau caps small ac').get_text(strip = True)
data.append([name,country])
df = pd.DataFrame(data)
df
df is returning an empty list. Not sure what is going on. When I take the code out of the for loop, it can't seem to find the select_one function. Function should be coming from bs4 - not sure why that's not working. Also, is there a repeatable pattern for web scraping that I'm missing? Seems like it's a different beast every time I try to tackle these kinds of problems.
Any help would be appreciated! Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
url 的数据是由 javascript 动态生成的,Beautifulsoup 无法抓取动态数据。因此,您可以使用自动化工具(例如 selenium 和 Beautifulsoup)。这里我将 selenium 与 Beautifulsoup 一起使用。请运行代码。
脚本:
输出:
The url's data is generated dynamically by javascript and Beautifulsoup can't grab dynamic data.So, You can use automation tool something like selenium with Beautifulsoup.Here I apply selenium with Beautifulsoup.Please just run the code.
Script:
Output:
该页面是使用 JavaScript 动态加载的,因此请求无法直接访问该页面。数据是从另一个地址加载的,并以json格式接收。您可以通过以下方式获取它:
加载后,您可以迭代它并检索相关信息。例如:
输出:
等。
然后您可以将输出加载到 pandas 数据框或其他内容。
The page is dynamically loaded using javascript, so requests can't get to it directly. The data is loaded from another address and is received in json format. You can get to it this way:
Once you have it loaded, you can iterate through it and retrieve relevant information. For example:
Output:
etc.
You can then load the output to a pandas dataframe or whatever.