如何使用 BeautifulSoup 抓取特定的谷歌天气文本?
P粉275883973
P粉275883973 2024-04-01 14:06:14
0
1
451

如何使用 BeautifulSoupPython 中找到课程文本“美国纽约市”?

尝试复制视频进行练习,但不再有效。

尝试在官方文档中找到一些内容,但没有成功。或者我的 get_html_content 函数无法正常工作,Google 只是阻止我,从而返回一个空的 list / None

这是我当前的代码:

from django.shortcuts import render
import requests

def get_html_content(city):
    USER_AGENT = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36"
    LANGUAGE = "en-US,en;q=0.5"
    session = requests.Session()
    session.headers['User-Agent'] = USER_AGENT
    session.headers['Accept-Language'] = LANGUAGE
    session.headers['Content-Language'] = LANGUAGE
    city.replace(" ", "+")
    html_content = session.get(f"https://www.google.com/search?q=weather+in+{city}").text
    return html_content

def home(request):
    result = None
    if 'city' in request.GET: 
        city = request.GET.get('city')
        html_content = get_html_content(city)
        from bs4 import BeautifulSoup
        soup = BeautifulSoup(html_content, 'html.parser')
        soup.find_all('div', attrs={'class': 'wob_loc q8U8x'})
        **OR**
        soup.find_all('div', attrs={'id': 'wob_loc'})

--> 都返回空列表(= .find 方法返回 None

P粉275883973
P粉275883973

全部回复(1)
P粉509383150

Google 页面的布局可能同时发生了变化,因此要获取有关天气的数据,您必须更改代码。例如:

import requests
from bs4 import BeautifulSoup


params = {'q':'weather in New York City, New York, USA', 'hl': 'en'}
headers = {'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:108.0) Gecko/20100101 Firefox/108.0'}
cookies = {'CONSENT':"YES+cb.20220419-08-p0.cs+FX+111"}

url = 'https://www.google.com/search'


soup = BeautifulSoup(requests.get(url, params=params, headers=headers, cookies=cookies).content, 'html.parser')

for t in soup.select('#wob_dp [aria-label]'):
    how = t.find_next('img')['alt']
    temp = t.find_next('span').get_text(strip=True)
    print('{:

打印:

Mon   Sunny                8
Tue   Cloudy               7
Wed   Partly cloudy        11
Thu   Rain                 7
Fri   Mostly cloudy        8
Sat   Partly cloudy        6
Sun   Scattered showers    8
Mon   Showers              8
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板