使用python bs4基于标题的屏幕抓取
我在使用bs4进行屏幕抓取时遇到问题。下面是我的代码使用python bs4基于标题的屏幕抓取,python,web-scraping,beautifulsoup,Python,Web Scraping,Beautifulsoup,我在使用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'}
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间卧室,位于Velachery的住宅公寓
,用于所有锚链。但是我没有得到任何输出,也没有错误。那么,我该如何告诉程序清除所有标题中包含单词“卧房”
的数据呢
希望我说得很清楚。您需要在此处使用正则表达式,因为您只希望匹配那些在标题中包含
卧房
的锚链接,而不是整个标题:
import re
properties = soup.find_all('a', title=re.compile('Bedroom'))
这为您给定的URL提供了47个匹配项