标签: beautifulsoup

BeautifulSoup 无法在 google colab 中工作

我在 google colab 中尝试了以下代码,但它给出了一个错误,我也在下面显示了该错误

!pip install beautifulsoup4
Run Code Online (Sandbox Code Playgroud)

已满足要求: /usr/local/lib/python3.7/dist-packages 中的 beautifulsoup4 (4.6.3)

from beautifulsoup4 import BeautifulSoup
Run Code Online (Sandbox Code Playgroud)

ModuleNotFoundError Traceback(最近一次调用最后一次) in () ----> 1 from beautifulsoup4 import BeautifulSoup

ModuleNotFoundError:没有名为“beautifulsoup4”的模块

-------------------------------------------------- ------------------------- 注意:如果由于缺少软件包而导致导入失败,您可以使用 !pip 或 !apt 手动安装依赖项。

要查看安装一些常见依赖项的示例,请单击下面的“打开示例”按钮。


比我也尝试过

!apt install BeautifulSoup4
Run Code Online (Sandbox Code Playgroud)

E: 无法找到包 BeautifulSoup4

!apt install BeautifulSoup
Run Code Online (Sandbox Code Playgroud)

E: 无法找到包 BeautifulSoup

我也尝试了上面的命令,没有感叹号(!)。其他软件包已安装,但 beautifulsoup4 未安装,也不允许导入。

如果有人能在这方面指导我,我将不胜感激。

python beautifulsoup google-colaboratory

1
推荐指数
1
解决办法
9070
查看次数

从 forexfactory.com 抓取数据

我是Python的初学者。在这个问题中,他们从外汇工厂提取数据。当时的解决方案是按照他们的逻辑工作,找到 table soup.find('table', class_="calendar__table")。但是,现在网络结构已经改变,html table is removed and converted to some javascript format. 所以,这个解决方案现在找不到任何东西。

 import requests
from bs4 import BeautifulSoup

r = requests.get('http://www.forexfactory.com/calendar.php?day=nov18.2016')
soup = BeautifulSoup(r.text, 'lxml')

calendar_table = soup.find('table', class_="calendar__table")

print(calendar_table)


# for row in calendar_table.find_all('tr', class_=['calendar__row calendar_row','newday']):
#     row_data = [td.get_text(strip=True) for td in row.find_all('td')]
#     print(row_data)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

由于我是初学者,我不知道该怎么做。那么,我该如何抓取数据呢?如果您给我任何提示,这对我会有帮助。非常感谢您阅读我的帖子。

python selenium beautifulsoup web-scraping python-3.x

1
推荐指数
2
解决办法
3852
查看次数

使用 BeautifulSoup 和 Python 从任何网页中提取主要文本

我正在编写一些代码(Python)来从网页中抓取文本。我的目标是找到一种方法来过滤/删除网页上不在主文章中的段落(例如广告、其他文章的链接等)。

到目前为止,我一直在使用该.find_all("p")命令仅从文本中提取段落,虽然成功,但也删除了许多不在每篇文章的主体/正文中的基本段落。这是我现在的代码:

from urllib.request import Request, urlopen
from bs4 import BeautifulSoup

URLs = [
"https://www.elsoldetoluca.com.mx/local/proponen-sistemas-para-captar-agua-pluvial-en-el-edomex-6585661.html",
"https://www.elsoldetoluca.com.mx/local/agua-de-acuifero-del-valle-de-toluca-solo-debe-ser-para-uso-de-consumo-humano-especialista-4146232.html"
        ]

for url in URLs:
    req = Request(url, headers={"User-Agent": 'Mozilla/5.0'})
    page = urlopen(req)
    paragraphs = []
    htmlParse = BeautifulSoup(page.read(), 'lxml')    
    for para in htmlParse.find_all("p"):
        paragraph = para.get_text().replace("\n", " ")
        paragraphs = paragraphs + [paragraph]
         
    text  = str("\n\n".join(paragraphs)) 
Run Code Online (Sandbox Code Playgroud)

所以我正在寻找一种聪明的方法来过滤掉主文章中没有的段落。重要的是,此方法可以应用于任何网页,因为我在大约 100 个随机网站上使用此代码。我已经研究过的事情是过滤包含某些单词的段落,但是,我宁愿不这样做,因为这样会遗漏很多信息/段落。另外,我一直在考虑省略具有某些名称的 HTML 部分(例如https://matix.io/extract-text-from-webpage-using-beautifulsoup-and-python/),但我发现这不是非常有效...

有人知道如何优雅地做到这一点吗?谢谢!

html python beautifulsoup web-scraping

1
推荐指数
1
解决办法
4116
查看次数

为什么通过 beautiful soup 网络抓取股票价格返回的价格与雅虎财经页面上的价格不同?

我正在尝试编写一个程序,该程序将为我提供一些不同股票的股价,但是当我运行我的程序时,它返回 116.71,而雅虎财经在页面和 HTML 中将其显示为 117.96(在写这个)。知道发生了什么事吗?页面在这里。代码如下:

from bs4 import BeautifulSoup
import requests


url = 'https://finance.yahoo.com/quote/VTSAX?p=VTSAX&.tsrc=fin-srch'
page = requests.get(url)
soup = BeautifulSoup(page.text, 'html.parser')
price = soup.find('fin-streamer', {'class': 'Fw(b) Fz(36px) Mb(-4px) D(ib)'}).text

print(price)
Run Code Online (Sandbox Code Playgroud)

python beautifulsoup web-scraping yahoo-finance

1
推荐指数
1
解决办法
867
查看次数

Beautiful Soup findAll 找不到价值

我想用 beautifulsoup 使用下面的代码编写该网站上产品的价格,但是当我编写代码时,列表返回空。

将请求导入为 req from bs4 将 BeautifulSoup 导入为 bs

url =“https://www.migros.com.tr/temel-gida-c-2?sayfa=1”

headers = { 'User-Agent': ( 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) ' 'AppleWebKit/537.36 (KHTML,如 Gecko) Chrome/39.0.2171.95 Safari/537.36' ) }

urrunler = []

rx = req.get(url, headers=headers) sympo = bs(rx.text, 'html.parser') soup = bs(rx.content, 'lxml') print(sympo.findAll('span', {'类': '金额'}))

print(sympo.findAll('span', {'class': 'amount'}))
[]
Run Code Online (Sandbox Code Playgroud)

html python beautifulsoup web-scraping

1
推荐指数
1
解决办法
662
查看次数

无法网页抓取 html 表漂亮的汤

尝试从这里废弃 IPO 表数据: https://www.iposcoop.com/last-12-months/

这是我的代码:

import requests
from bs4 import BeautifulSoup
import pandas as pd

url = 'https://www.iposcoop.com/last-12-months/'
page = requests.get(url)
soup = BeautifulSoup(page.text, 'lxml')
table1 = soup.find("table",id='DataTables_Table_0')
table1_data = table1.tbody.find_all("tr")
table1
Run Code Online (Sandbox Code Playgroud)

但是,table1 是 NonType。这是为什么?有什么解决办法吗?我读过相关问题,iframe 似乎不是答案。

python beautifulsoup web-scraping

1
推荐指数
1
解决办法
169
查看次数

Python beautifulsoup - 获取由中断标记分隔的所有文本

我有以下表格:

<table width="100%" border="0" cellspacing="2" cellpadding="0">
                      <tbody><tr> 
                        <td class="labelplain">ANGARA, EDGARDO J.<br>ENRILE, JUAN PONCE<br>MAGSAYSAY JR., RAMON B.<br>ROXAS, MAR<br>GORDON, RICHARD "DICK" J.<br>FLAVIER, JUAN M.<br>MADRIGAL, M. A.<br>ARROYO, JOKER P.<br>RECTO, RALPH G.<br></td>
                      </tr>
                    </tbody></table>
Run Code Online (Sandbox Code Playgroud)

我可以使用下面的代码遍历 HTML 的这一部分:

soup.find('td', text = re.compile('Co-author\(s'), attrs={'class': 'labelplain'}).find_next('td')
coauthor = soup.find('td', text = re.compile('Co-author\(s'), attrs={'class': 'labelplain'}).find_next('td')
Run Code Online (Sandbox Code Playgroud)

我可以使用以下内容获取文本:

for br in coauthor.find_all('br'):
  firstcoauthor = (br.previousSibling)
  print (firstcoauthor)
Run Code Online (Sandbox Code Playgroud)

我想要得到的输出是所有文本的结果,然后用分号(;)分隔,如下所示:ANGARA, EDGARDO J.;ENRILE, JUAN PONCE;MAGSAYSAY JR., RAMON B.;ROXAS, MAR;GORDON, RICHARD “迪克” J.;弗拉维尔,胡安 M.;马德里加尔,马萨诸塞州;阿罗约,小丑 P.;莱克托,拉尔夫 G.

但上面的代码给了我如下结果:

ANGARA, EDGARDO J.
ENRILE, JUAN PONCE
MAGSAYSAY …
Run Code Online (Sandbox Code Playgroud)

python string replace beautifulsoup web-scraping

1
推荐指数
1
解决办法
1044
查看次数

从具有复杂逻辑的单列创建多列

我对数据框和/或系列的所有 apply、applymap、map 内容感到有点困惑。我想通过执行一些网络抓取操作的函数创建从数据框中的一列派生的多个列。

我的数据框看起来像这样

>>> df
          row1        url    row3
0        data1  http://...    123
1        data2  http://...    325
2        data3  http://...    346
Run Code Online (Sandbox Code Playgroud)

网页抓取功能是这样的

def get_stuff_from_url(url: str):
    response = requests.get(url)
    soup = BeautifulSoup(response.text, 'html.parser')
    data1 = soup.find('div', {'class': 'stuff1'})
    data2 = soup.find('span', {'class', 'stuff2'}).text
    data3 = soup.find('p', {'class', 'stuff3'}).text

    return data1, data2, data3
Run Code Online (Sandbox Code Playgroud)

结果应该是

>>> df_new
          row1        url    row3       row4       row5       row6
0        data1  http://...    123  newdata1a  newdata2a  newdata3a
1        data2  http://...    325  newdata1b  newdata2b  newdata3b
2        data3  http://...    346  newdata1c  newdata2c …
Run Code Online (Sandbox Code Playgroud)

python beautifulsoup dataframe web-scraping pandas

1
推荐指数
1
解决办法
471
查看次数

Steam“请求购买”价格

要求购买价格

我想从蒸汽市场获得这个价格,但如果我尝试以这种方式获得

name = "P250 | Red Rock (Battle-Scarred)"
html = requests.get("https://steamcommunity.com/market/listings/730/"+name).text
soup = BeautifulSoup(html,"html5lib")
Run Code Online (Sandbox Code Playgroud)

我只得到 None 值。另一方面,我可以使用 Selenium,但它对我来说非常慢(一个请求近 3 秒)。如何得到这个号码?

python parsing beautifulsoup steam-web-api steam

1
推荐指数
1
解决办法
871
查看次数

Python beautifulsoup试图删除html标签'span'

我想删除

[<span class="street-address">
            510 E Airline Way
           </span>]
Run Code Online (Sandbox Code Playgroud)

我已经使用这个清洁功能来删除它们之间的那个 < >

def clean(val):
 if type(val) is not StringType: val = str(val)
 val = re.sub(r'<.*?>', '',val) 
 val = re.sub("\s+" , " ", val)
 return val.strip()
Run Code Online (Sandbox Code Playgroud)

它产生了 [ 510 E Airline Way ]

我试图内"干净"的功能添加到删除字符'['']',基本上我只是想要得到的"510 E Airline Way".

任何人都有任何线索我可以添加什么clean功能?

谢谢

python regex beautifulsoup

0
推荐指数
1
解决办法
3675
查看次数