【问题标题】:Selecting URLs to print from multiple DIVs从多个 DIV 中选择要打印的 URL
【发布时间】: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&amp;pID=10727**">Janet Gilligan Abaray</a></li>
<li>
<a href="**/Pages/MemberProfile.aspx?sST=OH&amp;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


    【解决方案1】:

    可以在href属性中获取所有包含MemberProfilea元素:

    from bs4 import BeautifulSoup
    import requests
    
    url = 'https://www.ohiobar.org/Pages/Find-a-Lawyer.aspx?sFN=&sLN=&sPA=&sCI=&sST=OH&sZC='
    
    with requests.Session() as session:
        session.headers = {'User-Agent': 'Mozilla/5.0 (Linux; U; Android 4.0.3; ko-kr; LG-L160L Build/IML74K) AppleWebkit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30'}
    
        response = session.get(url)
        soup = BeautifulSoup(response.content)
    
        for link in soup.select("div#content_findResults div#content_column1 ul li a[href*=MemberProfile]"):
            print link.get("href")
    

    在这里,我使用CSS selector 来定位a 元素。

    打印:

    /Pages/MemberProfile.aspx?sST=OH&pID=10727
    /Pages/MemberProfile.aspx?sST=OH&pID=26507
    ...
    /Pages/MemberProfile.aspx?sST=OH&pID=17139
    /Pages/MemberProfile.aspx?sST=OH&pID=57207
    

    【讨论】:

    • 非常感谢。有几个问题,如果可以的话:- 1. 我从哪里得到用户代理信息来输入? 2. 我发布的 URL 有超过 18K 的 URL,但代码只返回大约 1600。如何从页面中提取“所有”URL。没有分页,所有的 URL 都只在那个页面上。再次感谢您
    • @pb_ng 用户代理可能不需要设置。我刚刚使用了类似请求+bs4 代码 sn-ps 的模板。如果您遇到困难,您的第二个问题最好在单独的线程中解决。考虑创建一个新问题并在此处给我一个链接。
    • @pb_ng 好吧,我可能马上就知道答案了。安装lxml 并尝试使用soup = BeautifulSoup(response.content, "lxml")。 Lmk 是否有帮助。谢谢。
    • 感谢 Alex,但它得到了相同数量的结果。还有什么我应该尝试的吗?
    • @pb_ng 有趣,我得到 18440。您执行的代码是否与答案中提供的完全相同?谢谢。
    猜你喜欢
    • 1970-01-01
    • 2012-09-01
    • 1970-01-01
    • 2018-11-03
    • 1970-01-01
    • 1970-01-01
    • 2021-05-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多