我正在尝试使用此网站 https://www.timeanddate.com/weather/通过打开如下 URL 使用 BeautifulSoup4 抓取天气数据:
quote_page=r"https://www.timeanddate.com/weather/%s/%s/ext" %(country, place)
我对网络抓取方法还是新手,BS4
,我可以在页面源中找到我需要的信息(例如,我们在此搜索中将国家/地区设为印度,将城市设为孟买)链接为:https://www.timeanddate.com/weather/india/mumbai/ext
如果你看到页面的源码,使用 CTRL+F
并不难。并找到“湿度”、“露点”和当前天气状态(如果晴天、下雨等)等信息的属性,唯一阻止我获取这些数据的是我对 BS4
的了解。 。
您可以检查页面源代码并写入 BS4
获取信息的方法,例如
“感觉像:”、“能见度”、“露点”、“湿度”、“风”和“预报”?
注意:我之前做过数据抓取练习,必须获取 HTML 标记中的值,如 <tag class="someclass">value</tag>
使用
`
a=BeautifulSoup.find(tag, attrs={'class':'someclass'})
a=a.text.strip()`
最佳答案
您可以熟悉 css 选择器
import requests
from bs4 import BeautifulSoup as bs
country = 'india'
place = 'mumbai'
headers = {'User-Agent' : 'Mozilla/5.0',
'Host' : 'www.timeanddate.com'}
quote_page= 'https://www.timeanddate.com/weather/{0}/{1}'.format(country, place)
res = requests.get(quote_page)
soup = bs(res.content, 'lxml')
firstItem = soup.select_one('#qlook p:nth-of-type(2)')
strings = [string for string in firstItem.stripped_strings]
feelsLike = strings[0]
print(feelsLike)
quickFacts = [item.text for item in soup.select('#qfacts p')]
for fact in quickFacts:
print(fact)
<小时/>
第一个选择器#qlook p:nth-of-type(2)
使用 id selector指定父级然后 :nth-of-type CSS pseudo-class选择其中的第二个段落类型元素(p 标签)。
该选择器匹配:
我使用 stripped_strings
来分离各个行并通过索引访问所需的信息。
第二个选择器#qfacts p
使用id selector为父元素,然后是 descendant combinator与p
type selector指定子 p 标签元素。该组合符合以下条件:
quickFacts
表示这些匹配项的列表。您可以通过索引访问项目。
关于python - 获取一个国家的天气,地点 bs4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55077185/