我试图抓住文章和标题,但有一部分我只是不知道解析出来。你知道吗
url = "http://insideevs.com/"
page = requests.get(url)
data = page.text
soup = BeautifulSoup(data, "lxml")
latest = []
b = soup.find_all('div', class_=re.compile("content"))
for a in b:
latest.append(a.get_text(strip=True))
例如,列表中的所有项目都有一个时间框架和附加到文章的评论。”两周前,574条评论“等。有人能告诉我如何排除这些最后的片段吗?你知道吗
首先使用BeautifulSoup来获取
h3
元素的集合,这些元素具有所需的数据项或与之相邻。我说相邻是因为其中一个缩写文本在每种情况下都是h3
的兄弟。你知道吗现在,在
h3
项中,您可以再次使用select
方法来查找其中的a
链接元素并获取其text
。您想要的文本是link元素的同级;但是,它只是几个元素中的一个,因此我使用:nth-of-type(1)
请求第一个。差点忘了~ p
说,“把我的兄弟姐妹给我”,不管我用什么称呼,碰巧是h3
。你知道吗然后,我们可以通过请求链接的
href
属性来获得全文的链接,而在我们请求链接的text
属性之前。你知道吗我将所有这些放在一个
enumerate
中,这样我就可以整齐地安排将输出从页面中截断为五个项目。你知道吗使用
extract
删除所需的标记。你知道吗代码示例:
相关问题 更多 >
编程相关推荐