如何使用 BeautifulSoup 在 Python 中找到课程文本“美国纽约市”?
尝试复制视频进行练习,但不再有效。
尝试在官方文档中找到一些内容,但没有成功。或者我的 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)
Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号