第二个刮刀 - 如果声明

时间:2018-01-29 23:53:43

标签: html python-3.x if-statement web-scraping beautifulsoup

我正在研究我的第二个Python scraper并继续遇到同样的问题。我想刮掉下面代码中显示的网站。我希望能够输入宗地号码,看看他们的物业使用代码是否匹配。但是,我不确定我的刮刀是否在表中找到了正确的行。另外,如果使用代码不是3730,则不确定如何使用if语句。

任何帮助都将不胜感激。

from bs4 import BeautifulSoup
import requests
parcel = input("Parcel Number: ")
web = "https://mcassessor.maricopa.gov/mcs.php?q="
web_page = web+parcel
web_header={'User-Agent':'Mozilla/5.0(Macintosh;IntelMacOSX10_13_2)AppleWebKit/537.36(KHTML,likeGecko)Chrome/63.0.3239.132Safari/537.36'}
response=requests.get(web_page,headers=web_header,timeout=100)
soup=BeautifulSoup(response.content,'html.parser')
table=soup.find("td", class_="Property Use Code" )
first_row=table.find_all("td")[1]
if first_row is '3730':
    print (parcel)
else:
   print ('N/A')

1 个答案:

答案 0 :(得分:0)

你正在看的html中没有类“属性使用代码” - 这是td的文本。如果要查找该行,可以使用

td = soup.find('td', text="Property Use Code")

然后,要获得该行中的下一个td,您可以使用:

otherTd = td.find_next_sibling()

或者,你想要他们所有人:

otherTds = td.find_next_siblings()

我不清楚您想要对这些tds的值做什么,但您需要使用text属性来访问它们:您的first_row is '3730'将始终为False,因为first_row此处为bs4.element.Tag个对象,'3730'str。但是,您可以从otherTd.text == '3730'获取有用的信息。