网页抓取返回空

时间:2016-02-16 01:47:17

标签: python web-scraping beautifulsoup lxml

我第一次尝试学习拼抢。我想知道美国国会议员的正式名字。

我成功完成了POST - response.content确实是完整的html字符串。但不知何故,lxmlbs4并没有帮助我取名。

这是一个简短的例子,搜索姓氏" Waxman"在this site。我想要的结果是该人的全名,如表中所述。我做了Inspect Element>在名称上复制XPATH。

from lxml import html
import requests

shortname = 'WAXMAN'
state = 'California'
chamber = 'House'

url = 'http://bioguide.congress.gov/biosearch/biosearch1.asp'
formData = {'lastname': shortname}

response = requests.post(url, data=formData)
tree = html.fromstring(response.content)
print tree.xpath('/html/body/center/table/tbody/tr[1]/td[1]/a/text()')

我对beautifulSoup的尝试也不起作用,但我对该软件包不熟悉。

from bs4 import BeautifulSoup
soup = BeautifulSoup(response.content, "lxml")
soup.select('body > center > table > tbody > tr:nth-child(2) > td:nth-child(1) > a')

1 个答案:

答案 0 :(得分:1)

您可以简化表达:

//table//td/a/text()

正在打印['WAXMAN, Henry Arnold']的结果。