【问题标题】:Scrape Web Pages with Beautiful Soup and Python 3使用 Beautiful Soup 和 Python 3 抓取网页
【发布时间】:2018-11-14 20:19:23
【问题描述】:

开始学习python 3和漂亮的soap 尝试使用以下代码从网页获取结果:

import mechanicalsoup
from bs4 import BeautifulSoup
browser = mechanicalsoup.StatefulBrowser()
browser.open("http://www.intellicast.com/")
browser.select_form()
browser["query"] = input("Enter city,Country")
response = browser.submit_selected()
html = response.text
soup = BeautifulSoup(html, features="lxml")
right_table = soup.find_all('td', id="conditions")
print(right_table)

我被困在这一点上 基于用户输入的结果应该是这样的:

32°F 感觉像:23° 风寒:23°
天花板:5400 热指数:32°
能见度:10mi 露点:16°
风速:12mph 湿度:51%
方向:330°NNW 压力:30.53"
阵风:17mph

如何得到这个结果,请帮忙。

提前致谢。

【问题讨论】:

  • 请问哪些用户输入值产生了上述内容?
  • 在加拿大多伦多前随机输入

标签: python html web-scraping beautifulsoup


【解决方案1】:

从html中删除<!--[if lte IE 9]><![endif]-->.select()右元素的注释

...
html = response.text
html = html.replace('<!--[if lte IE 9]>', '').replace('<![endif]-->', '')
soup = BeautifulSoup(html, features="lxml")
right_table = soup.select('#conditions tr')
for tr in right_table:
    print(tr.text.replace('\n', ''))

【讨论】:

  • 不错的答案.. 你会一直这样吗?
  • 我正在尝试在用户输入后从同一网站获取 10 天的天气预报。我无法抓取 html。请帮忙。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-04
  • 1970-01-01
  • 1970-01-01
  • 2016-05-16
  • 1970-01-01
相关资源
最近更新 更多