【问题标题】:How do I breakup HTML elements in Beautifulsoup to insert them into a MySQL database table?如何分解 Beautifulsoup 中的 HTML 元素以将它们插入 MySQL 数据库表?
【发布时间】:2019-04-05 02:58:50
【问题描述】:

我正在使用 Beautifulsoup 从网站上抓取文本信息。如何只刮出选择文本,以便我可以将所选文本输入到 MySQL db?

我尝试了各种方法来提取文本字符串,但我没有任何运气。我得到了想要的输出,只是不确定如何消除不需要的项目。

from bs4 import BeautifulSoup
import urllib.request, urllib.parse, urllib.error

html_url = 'https://www.nwk.usace.army.mil/Locations/District-Lakes/Pomme-de-Terre-Lake/Daily-Lake-Info-2/' 

html_doc = urllib.request.urlopen(html_url).read() 

soup = BeautifulSoup(html_doc, 'html.parser')

pageNav = soup.find(class_= 'Normal')

pageSub = pageNav.find_all('p')

for strong_tag in soup.find_all('strong'):
    print (strong_tag.text, strong_tag.next_sibling)

我想限制并仅将四个项目拉入 MySQL DB:日期、昨天的最高温度、24 小时最低温度、湖面温度。这是我目前的结果:

日期:2019 年 4 月 3 日 正常池标高:839.00 湖海拔:843.53 24 小时变化:+0.14' 排放到 Pomme de Terre 河: Pomme de Terre River:每秒 50 立方英尺 气温 无 上午 8:00 观察到 48 昨天的高点:64 24 小时低:39 湖面温度:   46(周一和周五上午 8:00 左右拍摄) 河流温度:        46(周一和周五上午 8:00 左右拍摄) 风 无 方向: SE 速度:    4 峰值:9 降水量:0.00"

【问题讨论】:

  • 只用 if 语句过滤整个代码
  • 您对此有语法建议吗?

标签: python mysql beautifulsoup


【解决方案1】:

使用此代码:

from bs4 import BeautifulSoup
import urllib.request, urllib.parse, urllib.error

html_url = 'https://www.nwk.usace.army.mil/Locations/District-Lakes/Pomme-de-Terre-Lake/Daily-Lake-Info-2/' 

html_doc = urllib.request.urlopen(html_url).read() 

soup = BeautifulSoup(html_doc, 'html.parser')

pageNav = soup.find(class_= 'Normal')

pageSub = pageNav.find_all('p')

for strong_tag in soup.find_all('strong'):
    if strong_tag.text == "24 Hr. Change:" or strong_tag.text=="Yesterday's High:" or strong_tag.text=="Date:    " or strong_tag.text=="Lake Surface Temperature:":
        print(strong_tag.text, strong_tag.next_sibling)

if 语句应该对所有内容进行排序。我在 jupyter notebook 中尝试了这段代码,它工作正常。这里唯一的问题是单词 date 后面有一些空格。所以现在文件不会打印日期行。

要对日期大小写进行硬编码,请改用以下代码:

from bs4 import BeautifulSoup
import urllib.request, urllib.parse, urllib.error

html_url = 'https://www.nwk.usace.army.mil/Locations/District-Lakes/Pomme-de-Terre-Lake/Daily-Lake-Info-2/' 

html_doc = urllib.request.urlopen(html_url).read() 

soup = BeautifulSoup(html_doc, 'html.parser')

pageNav = soup.find(class_= 'Normal')

pageSub = pageNav.find_all('p')

date = True
for strong_tag in soup.find_all('strong'):
    if date:
        print(strong_tag.text, strong_tag.next_sibling)
        date = False
    if strong_tag.text == "24 Hr. Change:" or strong_tag.text=="Yesterday's High:" or strong_tag.text=="Lake Surface Temperature:":
        print(strong_tag.text, strong_tag.next_sibling)

【讨论】:

  • 结果相同:日期:2019 年 4 月 3 日 正常泳池海拔:839.00 湖海拔:843.53 24 小时。变化:+0.14' 排放到 Pomme de Terre 河: Pomme de Terre 河:每秒 50 立方英尺 气温在上午 8:00 无观测到 48 昨天的最高气温:64 24 小时最低气温:39 湖表面温度:46(周一和周五上午 8:00 左右拍摄) 河流温度:46(周一和周五上午 8:00 左右拍摄) 无风 方向:东南 速度:4 峰值:9 降水:0.00"
  • 您可以轻松地硬编码 date 属性,因为它是列表中的第一个元素。
猜你喜欢
  • 1970-01-01
  • 2013-10-17
  • 1970-01-01
  • 1970-01-01
  • 2019-01-12
  • 1970-01-01
  • 1970-01-01
  • 2012-10-12
  • 2018-01-04
相关资源
最近更新 更多