- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试从 this 获取信息地点。我本质上想从网站上获取所有可以获取的信息。所以整个 table 就很理想了。 html 中有一个 .DataTable
元素——我认为它应该很容易抓取。我在提取数据时遇到问题。
我首先尝试了一个简单的pandas
read_html
:
link = link
tables = pd.read_html(link,header=0)[0]
tables
这不起作用,我认为这是因为该网站实际上不是一个表格。所以我尝试使用 BeautifulSoup
,我认为这不会有问题:
import requests
from bs4 import BeautifulSoup
res = requests.get(link)
soup = BeautifulSoup(res.content, "html.parser")
tables = soup.find(".DataTable")
它不起作用。我认为会是因为 .DataTable
是我使用 SelectorGadget
时的元素。
我还尝试了以下方法:
res = requests.get(link)
soup = BeautifulSoup(res.content, "html.parser")
trs = soup.find("div", attrs={"class": "DataTable"}).find("table").findAll("tr")
for tr in trs:
tds = tr.findAll("td")
if len(tds) < 1:
continue
name = tds[0].text
print(name)
f.close()
这就是我开始脱离我的元素的地方。我不确定需要拉动什么杠杆才能使上述工作发挥作用。
接下来我应该尝试什么?有什么建议吗?
最佳答案
您可以从这个脚本开始。它对 .DataTable
进行一些基本的抓取:
import requests
from bs4 import BeautifulSoup
url = 'https://www.pluginillinois.org/OffersBegin.aspx'
with requests.session() as s:
soup = BeautifulSoup(s.get(url).text, 'lxml')
post_data = {i['name']: i['value'] for i in soup.select('input[name][value]')}
soup = BeautifulSoup(s.post(url, data=post_data).text, 'lxml')
data = []
for row in soup.select('.DataTable tr:has(td)'):
data += [ [td.get_text(separator=', ', strip=True).replace(', Compare', '') for td in row.select('td')] ]
from textwrap import shorten
for row in data:
for i, d in enumerate(row):
if i in (1, 2, 3, 4, 5, 6):
print('{: ^16}'.format(shorten(str(d[0]) if len(d) == 1 else str(d), 16)), end='|')
else:
print('{: <45}'.format(shorten(str(d[0]) if len(d) == 1 else str(d), 45)), end='|')
print()
打印:
1-800-755-5000 | Fixed [...] | $0 | $25.19 | $49.06 | $70.96 | |This Price to Compare is in effect from [...]|
877.808.1022, Three months fixed at [...] | Fixed [...] | 0 | $20.75 | $41.50 | $62.25 | 3, $0 |A low fixed electricity rate of 4.15 [...] |
888-354-4415, 12 Month Fixed | Fixed [...] | $0 | $28.40 | $56.80 | $85.20 | 12, $50 |An affordable 12 month fixed rate plan [...] |
(877) 282-6248, Illinois Select Variable Plan| Variable [...] | $0 | $38.50 | $77.00 | $115.50 | 0, $0 |Illinois Select Variable (Rate Zone I): [...]|
1-866-587-8674, 2-Year Price Protection Plan | Fixed [...] | $0 | $39.95 | $79.90 | $119.85 | 24, $50 |Lock in your electricity rate 24 months [...]|
1-866-769-3799, Fixed IL 24 | Fixed [...] | 0 | | | | 24, $50 |Our customer loyalty program is offered [...]|
AMEREN_FCBB_08 | Fixed [...] | 0 | $30.00 | $60.00 | $90.00 | 6, 0 |6 month fixed With a $50 Cash Back Bonus |
855-243-7270, Worry Free 24-month fixed | Fixed [...] | $0 | $39.67 | $79.33 | $119.00 | 24, $50 |Lock-in now for a worry free [...] |
866-663-2508, Value Fixed | Fixed [...] | $0 | $36.45 | $72.90 | $109.35 | 24, $50 |Value Fixed for 24 months! |
888-410-5081, 12-Month Green Fixed Rate | Fixed [...] | $0 | $40.58 | $81.16 | $121.74 | 12, $49 |Ilinois RPS requires that 14.5% of our [...] |
1-888-504-6372, Indra Web Only - Ameren [...]| Fixed [...] | 0 | $44.50 | $89.00 | $133.50 | 18, 0 |18 month fixed term contract with [...] |
(800)654-6990, GreenValueAssurance6Plus | Variable [...] | $9.99 | $25.45 | $50.89 | $76.35 | 6, 0 |GreenValueAssurancePlus is a 6 [...] |
866-663-2508, Value Fixed | Fixed [...] | $0 | $36.95 | $73.90 | $110.85 | 12, $50 |Value Fixed for 12 months! |
1-866-577-4700, 12 Month Plan | Fixed [...] | $0 | $27.95 | $55.90 | $83.85 | 12, $50 |You will receive a fixed price of 5.59¢ [...]|
844-684-5506, Fixed | Fixed [...] | $0 | $36.95 | $73.90 | $110.85 | 12, $50 |12 Month Fixed Rate with Price Certainty. |
1-855-331-0022, Live Brighter 12 | Fixed [...] | $0 | $29.45 | $58.90 | $88.35 | 12, $0 |A fixed electricity rate for 12 months [...] |
(877) 282-6248, Illinois Select 12 [...] | Fixed [...] | $0 | $24.45 | $48.90 | $73.35 | 12, $0 |Illinois Select 12 Month Term (Rate [...] |
888-504-6372, Indra Web Only - Ameren [...] | Fixed [...] | 0 | $35.50 | $71.00 | $106.50 | 18, 0 |Web Exclusive Fixed Green Plan: Initial [...]|
1-844-687-4519, Live Brighter 18 | Fixed [...] | $0 | $29.95 | $59.90 | $89.85 | 18, $0 |18 months of fixed-rate electricity! To [...]|
888-410-5081, 6-Month Fixed Rate | Fixed [...] | $0 | $29.99 | $59.99 | $89.98 | 6, $49 |We lock in your rate for 6 months. No [...] |
1-888-828-7374, Locked Ameren Res 12 mo [...]| Fixed [...] | 0 | $25.20 | $50.40 | $75.60 |12, $10.00 [...]|Residents Energy ResiSURE: Lock in a [...] |
(800)654-6990, GreenValueAssurance | Variable [...] | $9.99 | $31.45 | $62.90 | $94.35 | 5, 0 |GreenValueAssurance is a 5 month,100% [...] |
1-866-587-8674, Perks Points Plan | Fixed [...] | $0 | | | | 24, $50 |Sign up and receive up to 5,000 Just [...] |
1-888-828-7374, Locked Ameren Res 6 mo [...] | Fixed [...] | 0 | $24.85 | $49.70 | $74.55 |6, $10.00 [...] |Residents Energy ResiSURE: Lock in a [...] |
877-300-6747, Illinois ManagedPriceTM - [...]| Variable [...] | $3.75 | $25.55 | $47.35 | $69.15 | 24, $50 |The Illinois ManagedPriceTM is a [...] |
888-410-5081, 3-Month Fixed Rate | Fixed [...] | $0 | $24.95 | $49.90 | $74.85 | 3, $49 |We lock in your rate for 3 months. No [...] |
1-888-504-6372, Indra Web Only - Ameren [...]| Fixed [...] | 0 | $33.00 | $66.00 | $99.00 | 14, 0 |Web Exclusive Fixed Plan: Initial rate [...] |
877-648-1927, AEP Energy's Traditional [...] | Fixed [...] | 0 | $21.95 | $43.90 | $65.85 | 6, 0 |Includes access to AEP Energy Reward [...] |
(877) 648-1927, AEP Energy's [...] | Fixed [...] | $0 | $22.45 | $44.90 | $67.35 | 12, $0 |Includes access to AEP Energy Reward [...] |
844.692.4372, 12-Month Fixed - Agera [...] | Fixed [...] | $0 | $38.94 | $77.88 | $116.82 | 12, $50 |The Agera Fixed Advantage gives you [...] |
855-388-5276, Cash Back Electric Plan | Fixed [...] | $0 | $25.50 | $51.00 | $76.50 | 6, $0.00 |Get $50 sign up bonus after 6 months of [...]|
1-866-769-3799, Independence 18 | Fixed [...] | 0 | | | | 18, $50 |Our customer loyalty program is offered [...]|
(877) 282-6248, Illinois Select Green [...] | Variable [...] | $0 | $48.60 | $97.20 | $145.80 | 0, $0 |Illinois Select Green Variable (Rate [...] |
1-888-504-6372, Indra Web Only - Ameren [...]| Fixed [...] | 0 | $41.00 | $82.00 | $123.00 | 14, 0 |Web Exclusive 14 month fixed contract: [...] |
855-243-7270, Worry Free 12-month fixed | Fixed [...] | $0 | $39.29 | $78.57 | $117.86 | 12, $50 |Lock-in now for a worry free [...] |
(877) 648-1927, AEP Energy's Renewable [...] | Fixed [...] | $0 | $22.95 | $45.90 | $68.85 | 12, $0 |Includes access to AEP Energy Reward [...] |
1-800-654-6990, ClearCharge12 EV | Fixed [...] | 0 | $34.45 | $68.90 | $103.35 | 12, $50 |ClearCharge12 EV is a 12 month,100% [...] |
1-866-577-4700, 12 Month Plan - 100% Green | Fixed [...] | $0 | $28.45 | $56.90 | $85.35 | 12, $50 |Receive a fixed price of 5.69¢ per kWh [...] |
Go Green Lights 24 | Fixed [...] | 0 | $30.95 | $61.90 | $92.85 | 24, 0 |Reducing your carbon footprint is [...] |
(877) 282-6248, Illinois Select Green Term | Fixed [...] | $0 | $34.45 | $68.90 | $103.35 | 12, $0 |Illinois Select Green 12 Month Term [...] |
888-354-4415, 24 Month Fixed | Fixed [...] | $0 | $29.30 | $58.60 | $87.90 | 24, $50 |An affordable 24 month fixed rate plan [...] |
855-388-5276, NRG 24M Price Lock Plan | Fixed [...] | $0 | $32.00 | $64.00 | $96.00 | 24, $0.00 |The Illinois Price Lock Plan includes [...] |
1-866-769-3799, Fixed IL 18 | Fixed [...] | 0 | | | | 18, $50 |Our customer loyalty program is offered [...]|
844.692.4372, 24-Month Fixed - Agera [...] | Fixed [...] | 0 | $39.28 | $78.56 | $117.84 | 24, 50 |The Agera Fixed Advantage gives you [...] |
1-800-654-6990, ClearGreenGuarantee 12 | Fixed [...] | None | $31.95 | $63.90 | $95.85 | 12, $50 |ClearGreenGuarantee 12 is a 12 [...] |
1-866-577-4700, 18 Month Plan | Fixed [...] | $0 | $28.45 | $56.90 | $85.35 | 18, $50 |Receive a fixed price of 5.69¢ per kWh [...] |
Power on Command 24 | Fixed [...] | 0 | $32.45 | $64.90 | $97.35 | 24, 0 |Lock in a competitive fixed electricity [...]|
8667057291, 12M Fixed Residential | Fixed [...] | $0 | $40.45 | $80.90 | $121.35 | 12, $50.00 |Our fixed rate program gives consumers [...] |
855-388-5276, Renewable Electric Plan | Fixed [...] | $0 | $26.50 | $53.00 | $79.50 | 6, $0.00 |100% National Wind Energy! Plus, get a [...] |
(800)654-6990, GreenValueAssurancePlus | Variable [...] | 0 | $27.95 | $55.90 | $83.85 | 6, 0 |GreenValueAssurancePlus is a 6 [...] |
1-855-331-0022, Live Brighter 17 | Fixed [...] | 0 | $27.45 | $54.90 | $82.35 | 17, 0 |A fixed electricity rate for 10 months [...] |
关于python - 使用 python 进行网页抓取 - 较旧的 html 设计。 .数据表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56975568/
如本answer所述,如果浏览器不支持 e,可以设置后备游标。 G。 光标:抓取;。我现在的问题是获取这些图像。在我的驱动器上本地搜索“.cur”只给了我系统光标,其中 grab.cur 和 grab
以下代码在计算机上运行以从 Instagram 帐户中抓取数据。当我尝试在 VPS 服务器上使用它时,我被重定向到 Instagram 登录页面,因此脚本不起作用。 为什么当我在电脑上或服务器上时,I
我在使用 Ruby 和 Mechanize 将 POST 查询传递到站点的网站上。访问站点的查询基于 firebug,如下所示 param.PrdNo=-1¶m.Type=Prop¶m
我正在尝试抓取一个具有多个页面结果的网站,例如“1、2、3、4、5...”。 每个分页号都是到另一个页面的链接,我需要抓取每个页面。 到目前为止,我想出了这个: while lien = page.l
我正在使用 HtmlAgilityPack 在 C# Asp.Net 中执行 Scraping,到目前为止,我在从多个 Web 执行 Scratch 时没有遇到问题,但是,尝试弹出以下代码时出现错误
如果我有一个 css 文件做这样的事情 #foo:after{content:"bar;} ,有没有办法用 javascript 获取 :after 的内容?获取父元素的内容只返回 #foo 元素的内
问题是这样的: 我有一个 Web 应用程序 - 一个经常更改的通知系统 - 在一系列本地计算机上运行。该应用程序每隔几秒刷新一次以显示新信息。计算机仅显示信息,没有键盘或任何输入设备。 问题是,如果与
我想制作一个程序来模拟用户浏览网站和点击链接。必须启用 Cookie 和 javascript。我已经在 python 中成功地做到了这一点,但我想把它写成一种可编译的语言(python ide 不会
我制作了这个小机器人,它通过搜索参数列表进行处理。它工作正常,直到页面上有几个结果: product_prices_euros 给出了一半为空的项目列表。因此,当我与 product_prices_c
我需要找到一个单词的匹配项,例如: 在网上找到所有单词“学习”https://www.georgetown.edu/(结果:4个字)(您可以看到它按CTRL + F并搜索) 我有我的 Python 代
有一个站点\资源提供一些一般统计信息以及搜索工具的界面。这种搜索操作成本高昂,因此我想限制频繁且连续(即自动)的搜索请求(来自人,而不是来自搜索引擎)。 我相信有很多现有的技术和框架可以执行一些情报抓
这并不是真正的抓取,我只是想在网页中找到类具有特定值的 URL。例如: 我想获取 href 值。关于如何做到这一点的任何想法?也许正则表达式?你能发布一些示例代码吗?我猜 html 抓取库,比如 B
我正在使用 scrapy。 我正在使用的网站具有无限滚动功能。 该网站有很多帖子,但我只抓取了 13 个。 如何抓取剩余的帖子? 这是我的代码: class exampleSpider(scrapy.
我正在尝试从这个 website 中抓取图像和新闻 url .我定义的标签是 root_tag=["div", {"class":"ngp_col ngp_col-bottom-gutter-2 ng
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭上个月。 Improve this ques
我在几个文件夹中有数千个 html 文件,我想从评论中提取数据并将其放入 csv 文件中。这将允许我为项目格式化和清理它。例如,我在这个文件夹中有 640 个 html 文件: D:\My Web S
我在编写用于抓取网页的实用程序时遇到了一个问题。 我正在发送 POST 请求来检索数据,我模仿我正在抓取的网络行为(根据使用 fiddler 收集的信息)。 我已经能够自动替换我的 POST 中除 V
对于 Googlebot 的 AJAX 抓取,我在我的网站中使用“_escaped_fragment_”参数。 现在我查看了 Yandex 对我网站的搜索结果。 我看到搜索结果中不存在 AJAX 响应
我正在尝试抓取网站的所有结果页面,它可以工作,但有时脚本会停止并显示此错误: 502 => Net::HTTPBadGateway for https://website.com/id/12/ --
我是一个学习网络爬虫的初学者,由于某种原因我无法爬网this地点。当我在 Chrome 中检查它时,代码看起来不错,但是当我用 BeautifulSoup 阅读它时,它不再是可刮的。汤提到“谷歌分析”
我是一名优秀的程序员,十分优秀!