【发布时间】:2018-05-02 12:45:14
【问题描述】:
我想在抓取的 html 页面中搜索特定的字符串,并在字符串存在时执行一些操作。
find = soup.find('word')
print(find)
但这会给出None,即使页面中有word。另外,我试过了:
find = soup.find_all('word')
print(find)
它只给[]。
【问题讨论】:
标签: python-3.x beautifulsoup scrapy
我想在抓取的 html 页面中搜索特定的字符串,并在字符串存在时执行一些操作。
find = soup.find('word')
print(find)
但这会给出None,即使页面中有word。另外,我试过了:
find = soup.find_all('word')
print(find)
它只给[]。
【问题讨论】:
标签: python-3.x beautifulsoup scrapy
find 方法的作用是搜索标签。因此,当您执行 soup.find('word') 时,您是在要求 BeautifulSoup 查找所有 <word></word> 标记。我认为这不是你想要的。
有几种方法可以执行您的要求。您可以使用re 模块通过这样的正则表达式进行搜索:
import re
is_present = bool(re.search('word', response.text))
但是您可以避免导入额外的模块,因为您使用 Scrapy,它具有用于处理正则表达式的内置方法。只需在选择器上使用re 方法:
is_present = bool(response.xpath('//body').re('word'))
【讨论】:
Response 对象,它作为第一个参数传递给你的回调函数。
is_present = bool(re.search('word', str(content)))
试试find = soup.findAll(text="word")
【讨论】: