[英]beautiful soup and parsing reddit
只是试图解析提交的 reddit 淋浴想法,但遇到了一个问题:
path = 'https://www.reddit.com/r/Showerthoughts/'
with requests.Session() as s:
r = s.get(path)
soup = BeautifulSoup(r.content, "lxml")
# print(soup.prettify())
threads = soup.find_all('p')
for thread in threads:
soup = thread
text = soup('a')
try:
print(text[0])
except:
pass
在这段代码中,我试图获取每个提交的标题,该标题包含在 < p > 标签中,然后是一个 < a > 标签,其类别为“title may-blank”。 但上面的代码返回所有带有 a 标签的元素,其中有很多甚至认为标题在那里我必须再经历两次soup.findAll()
,而且我确信有一种更少手动的搜索方式通过汤打印所有的标题
根据我的知识,我试图做
titles = soup.findAll( "a", {"class":"title may-blank}) for title in titles: print(title.string)
但这没有任何想法? PS 我知道这可以通过 reddit API 完成并且效率更高,但我想提高我的解析技能,因为它们不符合标准。 感谢您的帮助
它们是css类,您还需要添加一个用户代理:
import requests
from bs4 import BeautifulSoup
path = 'https://www.reddit.com/r/Showerthoughts/'
headers ={"User-Agent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36"}
with requests.Session() as s:
r = s.get(path, headers=headers)
soup = BeautifulSoup(r.content, "lxml")
threads = soup.select('a.title.may-blank')
for a in threads:
print(a)
你也可以使用soup.find_all("a", class_="title")
但这可能比你想要的更多。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.