如何从div获取文本?

时间:2019-07-07 16:51:17

标签: python web-scraping beautifulsoup

我想从一个网站上抓取信息,以获取有关用户的信息,以使漫游器存储在某个渠道中。

我看过教程,但是没有用。

import requests
from bs4 import BeautifulSoup

r = requests.get("http://www.rubyrealms.com/user/KOMKO190/")
content = r.content
soup = BeautifulSoup(content, "html.parser")
print(soup)

我如何从div获取DIV和文本?

1 个答案:

答案 0 :(得分:1)

我从页面上解析了一些示例项目,应该可以帮助您入门:

import requests
from bs4 import BeautifulSoup

r = requests.get("http://www.rubyrealms.com/user/KOMKO190/")
content = r.content
soup = BeautifulSoup(content, 'lxml')

name = soup.select_one('div.profile-title').text.strip()
about = soup.select_one('div.profile-about').text.strip()
achievements = [span['title'] for span in soup.select('div.achievements span[title]')]
stats = [div.text.strip() for div in soup.select('div.stats div')]

print('{: <20}{}'.format('Name:', name))
print('{: <20}{}'.format('About:', about))
print('{: <20}{}'.format('Achievements:', achievements))
print('{: <20}{}'.format('Stats:', stats))

打印:

Name:               KOMKO190
About:              Hey! My name is KOMKO190, you maybe know me from the forums or discord. I am a programmer, I know a bit of JavaScript, small portion of C++, Python and html/css. Mostly python. My user ID is 7364. ||| 5th owner of Space Helmet :)
Achievements:       ['Verified', 'Novice Collector', 'XP Prodigy', '1k Forum Posts']
Stats:              ['297 Profile Views', '1826 Forum Posts', 'Last seen 40 mins ago', 'Joined Apr 27, 2019']