Python\u Web\u scraping Html选项卡

2024-09-29 23:24:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我是Python的初学者,我还在学习阶段。 更具体地说,使用请求和bs4进行刮取。 尝试刮取以下链接时:“http://directorybtr.az.gov/listings/FirmSearchResults.asp?Zip%20Like%20%22850%25%22

我使用了以下代码:

import requests

from bs4 import BeautifulSoup
url ="http://directorybtr.az.gov/listings/FirmSearchResults.asp?Zip%20Like%20%22850%25%22"
res = requests.get(url)

soup = BeautifulSoup(res.text, 'html.parser')
res.close()
results = soup.find('table')

结果中没有表格,尽管在Chrome中检查源页面时该表格存在。 有什么解决办法或解释吗?你知道吗

谢谢


Tags: importhttpurlresrequests表格azgov
1条回答
网友
1楼 · 发布于 2024-09-29 23:24:48

表格数据在框架内,你需要先去

import requests
from lxml import html
from bs4 import BeautifulSoup
BASE_URL = "http://directorybtr.az.gov/listings/" 
URL = BASE_URL + "FirmSearchResults.asp?Zip%20Like%20%22850%25%22"
#u need session because the frame use the search results data, u cant directly go to Firms.asp
session = requests.session()
response = session.get(URL)
soup = BeautifulSoup(response.text, 'lxml')
#find the first frame 
frame = soup.find("frame")
#go to the frame link ( Firms.asp )
response = session.get(BASE_URL + frame.attrs['src'])
soup = BeautifulSoup(response.text, 'lxml')
table = soup.find("table")
print table
response.close()

相关问题 更多 >

    热门问题