我在使用bs4进行屏幕抓取时遇到问题。以下是我的代码。
from bs4 import BeautifulSoup
import urllib2
url="http://www.99acres.com/property-in-velachery-chennai-south-ffid?"
page=urllib2.urlopen(url)
soup = BeautifulSoup(page.read())
properties=soup.findAll('a',{'title':'Bedroom'})
for eachproperty in properties:
print eachproperty['href']+",", eachproperty.string
当我分析网站时,实际的标题结构如下所示
所有锚链接的 1 Bedroom, Residential Apartment in Velachery
。但我没有得到任何输出,也没有错误。那么如何告诉程序抓取标题中包含单词"Bedroom"
的所有数据?
希望我说清楚。
答案 0 :(得分:2)
您需要在此处使用正则表达式,因为您只想匹配标题中Bedroom
的锚链接,而不是整个标题:< / p>
import re
properties = soup.find_all('a', title=re.compile('Bedroom'))
这为您提供的网址提供了47场比赛。