繁体   English   中英

用 Beautiful Soup 和 Python 3 抓取网页

[英]Scrape Web Pages with Beautiful Soup and Python 3

开始学习python 3和beautiful 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

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

提前致谢。

从 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', ''))

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM