最大的目标是找到具体的房屋账单。
通过这段代码,我试图选择链接:/legislation?q=%7B%22congress%22%3A%22113%22%2C%22chamber%22%3A%22House%22%7D
来缩小我对房屋账单的搜索范围
from bs4 import BeautifulSoup
import urllib2
soup = BeautifulSoup(urllib2.urlopen("https://beta.congress.gov/legislation"))
for link in soup.find_all('a'):
soup_links = link.get('href')
import re
r1 = re.compile(r'/legislation(\?\S+congress\S+chamber\S+House\S+)')
print r1.findall(soup_links)
当我这样做时,我得到的是一个空列表而不是链接
这不是我的常规快车,因为以下几点很有效:
r2 = re.compile(r'\S+congress\S+chamber\S+House\S+')
newstring = '/legislation?q=%7B%22congress%22%3A%22113%22%2C%22chamber%22%3A%22House%22%7D'
print r2.findall(newstring)
您正在为每个迭代
soup_links
重新分配一个新值;最后只分配lasthref
属性BeautifulSoup可以为您搜索:
这将生成一个匹配的链接:
如果您只希望一个链接匹配,请使用
soup.find()
而不是soup.find_all()
:相关问题 更多 >
编程相关推荐