gpt4 book ai didi

python - 使用python抓取网页的问题

转载 作者:行者123 更新时间:2023-11-28 19:15:24 27 4
gpt4 key购买 nike

你好,我是 Python 的新手,我的老板让我抓取这些数据,但这不是我的强项,所以我想知道我将如何去做。

我查找的文本中的引号也会每隔几分钟更改一次,因此我也不确定如何找到它。

我目前正在使用 beautiful soup 和 Lxml,但是如果有更好的选择,我很乐意尝试它们

这是被检查的网页元素:

div class = "sometext"
<h3> somemoretext </h3>
<p>
<span class = "title" title="text i want">text i want</span>
<br>
</p>

我试过使用:

from lxml import html
import requests
from bs4 import BeautifulSoup
page = requests.get('the url')
soup = BeautifulSoup(page.text)
r = soup.findAll('//span[@class="title"]/text()')
print r

提前致谢,如有任何帮助,我们将不胜感激!

最佳答案

首先执行此操作以获得您在汤中看到的内容:

soup = BeautifulSoup(page)
print soup

这样您就可以仔细检查您实际处理的是您认为正在处理的内容。

然后这样做:

r = soup.findAll('span', attrs={"class":"title"})
for span in r:
print span.text

这将获得所有 span带有 class=title 的标签, 然后 text将打印出标签之间的所有文本。

编辑添加

请注意,esecules 的回答将为您提供 标签 ( <span class = "title" title="text i want"> ) 中的标题,而我的将从文本 ( <span class = "title" >text i want</span> ) 中获取标题

关于python - 使用python抓取网页的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34167176/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com