python - 获取一个国家的天气,地点 bs4

标签 python web-scraping beautifulsoup

我正在尝试使用此网站 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 标签)。

该选择器匹配:

enter image description here

我使用 stripped_strings 来分离各个行并通过索引访问所需的信息。

<小时/>

第二个选择器#qfacts p使用id selector为父元素,然后是 descendant combinatorp type selector指定子 p 标签元素。该组合符合以下条件:

quickFacts 表示这些匹配项的列表。您可以通过索引访问项目。

关于python - 获取一个国家的天气,地点 bs4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55077185/

相关文章:

python - 这些 python 代码示例之间的区别?

python - 将变量从命令行解析为 url

javascript - 在 puppeteer 请求中使用不同的 ip 地址

python - BeautifulSoup.get_text() 忽略换行符 <br>

python - django.core.exceptions 在 python 中运行 selenium 功能测试时出现 ImproperlyConfigured 错误

python - 接口(interface)错误 : connection already closed (using django + celery + Scrapy)

python - 如何忽略 Selenium 中的异常?

python - lxml/requests 可以选择下拉选项然后解析生成的ajax吗?

python - 使用 Python 将 div 元素内的文本位置计算为 HTML 中的相应位置

python - BeautifulSoup 返回空 html