简体   繁体   English

使用 Python 时无法在 chrome 开发工具中提取正确的元素

[英]Cannot extract correct element in chrome dev tools when using Python

I am attempting to access the dates from this site with css selectors but it is not allowing me.我正在尝试使用 css 选择器从该站点访问日期,但它不允许我。 I keep getting this error: AttributeError: 'NoneType' object has no attribute 'select'我不断收到此错误: AttributeError: 'NoneType' object has no attribute 'select'

import requests
from bs4 import BeautifulSoup
page = requests.get("https://www.accuweather.com/en/us/san- 
antonio/78205/daily-weather-forecast/351198")
soup = BeautifulSoup(page.content, 'html.parser')
daily = soup.find(class_="content-module")
period_tags = daily.select(".date .dow")
periods = [pt.get_text() for pt in period_tags]
periods

I expect the output to give me each of the days on the webpage in list form我希望 output 以列表形式在网页上的每一天给我

I only needed the one User-Agent header.我只需要一个用户代理 header。 However, content is dynamically generated so your requests response html won't be the same as on web-page where javascript can run.但是,内容是动态生成的,因此您的请求响应 html 不会与 javascript 可以运行的网页上的相同。 You can extract the required info from a script tag in the response using regex and then parse with json parser您可以使用正则表达式从响应中的script标记中提取所需信息,然后使用 json 解析器进行解析

import requests, re , json

headers = {'User-Agent': 'Mozilla/5.0'}
r = requests.get('https://www.accuweather.com/en/us/san-%20antonio/78205/daily-weather-forecast/351198', headers=headers)
p = re.compile(r'var dailyForecast = (.*);')
data = json.loads(p.findall(r.text)[0])
#print(data)
forecasts = {i['dow'] + ' - ' + i['date']:i['day'] for i in data}
print(forecasts)
dows  = [i['dow'] for i in data]
print(dows)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python:无法使用 Chrome 的开发工具检查浮动 cookie 元素,并且键盘操作正在此元素后面工作 - Python: Floating cookie element can't be inspected with Chrome's dev tools and keyboard actions are working behind this element 为什么从带有 javascript 的模板中看不到乘法的结果,但在 chrome 开发工具中检查元素时却看到了? - Why is the result of the multiplication not seen from the template with javascript but when inspecting the element in chrome dev tools it is? 无法提取<p> Python 中的元素使用 BeautifulSoup</p> - Cannot extract <p> element in Python using BeautifulSoup Python Selenium:使用 execute_cdp_cmd() 捕获 Chrome 开发工具网络请求/响应日志 - Python Selenium : Capture Chrome Dev Tools Network Request/Response Logs using execute_cdp_cmd() 尝试使用 Selenium 提取 Python 中的文本时无法定位元素 - Unable to locate element when attempting to extract a text in Python using Selenium 使用命令“python tools\dev\v8gen.py x64.release”时构建 v8 错误 - build v8 error when using command " python tools\dev\v8gen.py x64.release " xpath在chrome开发工具中有效,但在scrapy中无效 - xpath works in chrome dev tools, but not in scrapy Python selenium 使用 execute_cdp_cmd 访问 chrome 开发工具 | 确定哪个 stylesheetId 属于哪个样式表 - Python selenium accessing chrome dev tools with execute_cdp_cmd | Determine which stylesheetId belongs to which stylesheet Python:从网页中获取加载的资源(如 chrome 开发工具) - Python: Get loaded resources from a web page (like chrome Dev tools) 有没有办法通过 Python 加载网页的网络活动(您可以在 Chrome Dev Tools 上看到)? - Is there a way to get a webpage's Network activity (which you can see on Chrome Dev Tools) on load via Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM