【发布时间】:2015-07-21 07:08:37
【问题描述】:
我是编程和 Python 新手。
我正在使用 Python 2.7 和 BeautifulSoup 从某个搜索结果页面中提取所有 URL。
页面是https://www.ohiobar.org/Pages/Find-a-Lawyer.aspx?sFN=&sLN=&sPA=&sCI=&sST=OH&sZC=(可能需要一段时间才能加载)
围绕URL的代码如下:-
<div id="content_findResults">
<div id="content_column1">
<h1 id="ctl00_ctl45_g_1e68d58d_9902_48ce_b555_5d3eb35d5624_ctl00_headingCriteria">Showing Search Results for 'OH'</h1>
<h2 id="ctl00_ctl45_g_1e68d58d_9902_48ce_b555_5d3eb35d5624_ctl00_headingResults">Your search returned 18440 results</h2>
<h4 id="ctl00_ctl45_g_1e68d58d_9902_48ce_b555_5d3eb35d5624_ctl00_headingYourSearch">Your search: 'State: OH'</h4>
<ul id="ctl00_ctl45_g_1e68d58d_9902_48ce_b555_5d3eb35d5624_ctl00_resultsList">
<li>
<a href="**/Pages/MemberProfile.aspx?sST=OH&pID=10727**">Janet Gilligan Abaray</a></li>
<li>
<a href="**/Pages/MemberProfile.aspx?sST=OH&pID=26507**">Kenneth Pascal Abbarno</a></li>
我不知道用什么来确保我可以从多个 DIV、UL 和 LI 中提取 URls。
我正在使用以下内容:
def oh_crawler():
url = "https://www.ohiobar.org/Pages/Find-a-Lawyer.aspx?sFN=&sLN=&sPA=&sCI=&sST=OH&sZC="
code = requests.get(url)
text = code.text
soup = BeautifulSoup(text)
for link in soup.find('div',{'id':'content_findResult', 'id':'content_column1'},'a'):
href = 'https://www.ohiobar.org' + link.get('href')
print (href)
显然它不起作用。
请告知我如何选择要打印的 URL。
【问题讨论】:
标签: python python-2.7 web-scraping beautifulsoup html-parsing