【问题标题】:How to retrieve data within an a href tag如何在 href 标记中检索数据
【发布时间】:2016-12-18 19:24:33
【问题描述】:

大家好,我在爬网时遇到了一些困难。我正在尝试在嵌入在一些 html 中间的这段代码中获取 70,我的问题是我将如何去做。我尝试了各种方法,但似乎都没有。我正在使用 BeautifulSoup 模块并用 Python 3 编写。如果有人需要,我正在抓取的网站的链接很方便地是 href 中的链接。提前谢谢你。

<a href="http://www.accuweather.com/en/gb/london/ec4a-2/weather- forecast/328328">London, United Kingdom<span class="temp">70&deg;</span><span  class="icon i-33-s"></span></a>

from bs4 import*
import requests
data = requests.get("http://www.accuweather.com/en/gb/london/ec4a-2/weather-   forecast/328328")
soup = BeautifulSoup(data.text,"html.parser")

【问题讨论】:

    标签: python beautifulsoup web-crawler href


    【解决方案1】:
    from bs4 import BeautifulSoup
    import re
    import requests
    soup = BeautifulSoup(text,"html.parser")
    for link in soup.find("a")
       temp = link.find("span",{"class" : "temp"})
       print(re.findall(r"[0-9]{1,2}",temp.text))
    

    希望对你有帮助

    【讨论】:

    • 感谢您的评论!但这会打印出所有链接,并且我试图在该标签中获得“70”
    【解决方案2】:

    假设使用BeautifulSoup 不是严格要求,您可以使用html.parser 模块来做到这一点。以下是为您提到的用例定制的。 它获取两个数据字段,然后过滤掉数字。

    from html.parser import HTMLParser
    
    class MyHTMLParser(HTMLParser):
        def handle_data(self, data):
            if data.isdigit():
                print(data)
    
    parser = MyHTMLParser()
    
    parser.feed('<a href="http://www.accuweather.com/en/gb/london/ec4a-2/weather- forecast/328328">London, United Kingdom<span class="temp">70&deg;</span><span  class="icon i-33-s"></span></a>')
    

    这将输出70

    也可以使用正则表达式。

    【讨论】:

    • 也可以这样做,但我试图通过网络抓取一个天气网站,其中 70 是天气,而我发送的标签位于某些 html 的中间
    【解决方案3】:

    这将为您提供任何包含温度的跨度

    temps = soup.find_all('span',{'class':'temp'})
    

    然后循环遍历它

    for span in temps:
        temp = span.decode_contents() 
        # temp looks like "70&deg" or "70\xb0" so parse it
        print int(temp[:-1])
    

    如果你是在 python2 中,那么辛苦的工作可能是从 unicode 转换为 ASCII。

    但是 accu-weather 页面没有 temp 类的 span:

    In [12]: soup.select('[class~=temp]')
    Out[12]: 
    [<strong class="temp">19<span>\xb0</span></strong>,
     <strong class="temp">14<span>\xb0</span></strong>,
     <strong class="temp">24<span>\xb0</span></strong>,
     <strong class="temp">23<span>\xb0</span></strong>,
     <h2 class="temp">19\xb0</h2>,
     <h2 class="temp">19\xb0</h2>,
     <h2 class="temp">17\xb0</h2>,
     <h2 class="temp">19\xb0</h2>,
     <h2 class="temp">19\xb0</h2>,
     <h2 class="temp">19\xb0</h2>,
     <h2 class="temp">20\xb0</h2>,
     <h2 class="temp">19\xb0</h2>,
     <h2 class="temp">17\xb0</h2>,
     <h2 class="temp">19\xb0</h2>,
     <h2 class="temp">19\xb0</h2>]
    

    所以很难给你答案

    【讨论】:

    • 起初它看起来可以工作,但它没有
    • 当然,accu-weather 的那个页面不再使用带有 temp 类的 span。它使用 'h2' 和 'strong' 代替
    • 如果你得到正确的来源,它确实有一个带有临时类的跨度,如果你在浏览器中转到页面,你可以看到这个。
    • 它确实有一个名为 temp 的类
    【解决方案4】:

    您需要添加一个用户代理来获取正确的来源,然后使用您想要的标签/类名称进行选择:

    from bs4 import *
    import requests
    headers = {"user-agent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.82 Safari/537.36"}
    data = requests.get("http://www.accuweather.com/en/gb/london/ec4a-2/weather-forecast/328328", headers=headers)
    soup = BeautifulSoup(data.content)
    print(soup.select_one("span.local-temp").text)
    print([span.text for span in soup.select("span.temp")])
    

    如果我们运行代码,你会看到我们得到了我们需要的一切:

    In [17]: headers = {
       ....:     "user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.82 Safari/537.36"}
    
    In [18]: data = requests.get("http://www.accuweather.com/en/gb/london/ec4a-2/weather-forecast/328328", headers=headers)
    
    In [19]: soup = BeautifulSoup(data.content, "html.parser")
    
    In [20]: print(soup.find("span", "local-temp").text)
    18°C
    
    In [21]: print("\n".join([span.text for span in soup.select("span.temp")]))
    18°
    31°
    30°
    25°
    

    【讨论】:

    • 不用担心,当您右键单击并选择查看源代码时,总是可以在浏览器中检查从请求返回的源代码和实际源代码。
    • 不过我有几个问题,什么是用户代理以及为什么需要它。还有这些代码行做什么,soup.select_one("span.local-temp").text和 print([span.text for span in soup.select("span.temp")])
    • @goimpress,根据您的用户代理,您可以使用不同的内容en.wikipedia.org/wiki/User_agentspan.local-tempspan.temp 是 css 选择器,您可以使用 find("local-temp")find_all("span",{"class":"temp"}),我只是更喜欢使用选择器,crummy.com/software/BeautifulSoup/bs4/doc/#css-selectors
    猜你喜欢
    • 1970-01-01
    • 2020-01-20
    • 1970-01-01
    • 1970-01-01
    • 2018-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-03
    相关资源
    最近更新 更多