嘿,所有我在网络爬行时偶然发现了一些困难。我试图在这个嵌入在某些html中间的代码块中获得70,我的问题是我将如何去做。我已经尝试了各种方法但似乎没有工作。我使用BeautifulSoup模块并使用Python 3编写。如果有人需要,我可以方便地将链接添加到网站中。请提前告诉您。
<a href="http://www.accuweather.com/en/gb/london/ec4a-2/weather- forecast/328328">London, United Kingdom<span class="temp">70°</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")
答案 0 :(得分:0)
假设使用cmd='sudo -u ec2-user ls'
并非严格要求,您可以使用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°</span><span class="icon i-33-s"></span></a>')
也可以使用正则表达式完成。
答案 1 :(得分:0)
这将为您提供任何包含温度的范围
temps = soup.find_all('span',{'class':'temp'})
然后循环它
for span in temps:
temp = span.decode_contents()
# temp looks like "70°" or "70\xb0" so parse it
print int(temp[:-1])
如果你在python2中,那么艰苦的工作可能是从unicode转换为ASCII。
但是精确的天气页面没有类温度的范围:
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>]
所以很难给你一个答案
答案 2 :(得分:0)
您需要添加用户代理以获取正确的来源,然后使用您想要的标记/类名称进行选择:
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°
答案 3 :(得分:-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))
我希望这有助于你