标签: bs4

在beautifulsoup中抑制url的警告

我正在使用Beautiful Soup 4来解析一些从互联网上删除的HTML格式的文本.有时这个文本只是一些网站的链接.事实上,BS4非常关注:

UserWarning: "http://example.com" looks like a URL. Beautiful Soup is not
an HTTP client. You should probably use an HTTP client to get the document
behind the URL, and feed that document to Beautiful Soup.
Run Code Online (Sandbox Code Playgroud)

我非常清楚这一事实,我只想解释文字输入,而不是讲课.我使用控制台来监视脚本的活动,并且它被一个非常生气的库弄得乱七八糟.

有什么方法来抑制或禁用此警告?

python bs4

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

beautifulsoup和bs4有什么区别

我是python的新手,我试图解析一些XML文件,以便添加一些新的标签并存储新的XML文件.

python-beautifulsoup接缝是适合它的包装.在网上搜索教程,如何向BeautifulSoup解析的XML添加新标签,我发现,使用了包python-bs4.

查看包描述,两个包都具有相同的标题:

python-bs4 - error-tolerant HTML parser for Python
python-beautifulsoup - error-tolerant HTML parser for Python
Run Code Online (Sandbox Code Playgroud)

所以我的问题是:有什么区别?

//更新:如果有人意味着对这个问题进行投票,那么留下原因作为评论是有帮助的.

python xml beautifulsoup python-3.x bs4

15
推荐指数
3
解决办法
8189
查看次数

从存储的.html页面中提取新闻文章内容

我正在从html文件中读取文本并进行一些分析.这些.html文件是新闻文章.

码:

 html = open(filepath,'r').read()
 raw = nltk.clean_html(html)  
 raw.unidecode(item.decode('utf8'))
Run Code Online (Sandbox Code Playgroud)

现在我只想要文章内容,而不是广告,标题等其他文本.我怎么能在python中相对准确地这样做?

我知道一些像Jsoup(java api)和bolier这样的工具,但我想在python中这样做.我可以找到一些使用bs4的技术,但仅限于一种类型的页面.我有来自众多来源的新闻页面.此外,还缺少任何示例代码示例.

我在python中寻找与http://www.psl.cs.columbia.edu/wp-content/uploads/2011/03/3463-WWWJ.pdf完全相同的内容.

编辑: 为了更好地理解,请写一个示例代码来提取以下链接的内容http://www.nytimes.com/2015/05/19/health/study-finds-dense-breast-tissue-isnt-always -a-高癌症risk.html?SRC =我和REF =一般

python urllib2 bs4

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

使用BeautifulSoup从`img`标签中提取`src`属性

<div class="someClass">
    <a href="href">
        <img alt="some" src="some"/>
    </a>
</div>
Run Code Online (Sandbox Code Playgroud)

我使用bs4而我无法使用a.attrs['src']src,但我可以得到href.我该怎么办?

python regex bs4

13
推荐指数
4
解决办法
3万
查看次数

如何用Beautiful Soup找到所有评论

四年前就提出了这个问题,但现在BS4的答案已经过时了.

我想用漂亮的汤删除我的html文件中的所有评论.由于BS4将每个注释作为一种特殊类型的可导航字符串,我认为这段代码可以工作:

for comments in soup.find_all('comment'):
     comments.decompose()
Run Code Online (Sandbox Code Playgroud)

所以这不起作用....如何使用BS4找到所有评论?

html python comments beautifulsoup bs4

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

更改 HTML 标签内的属性以查看完整内容 Python BeautifulSoup

我正在尝试查看网站 Fortune.com/best-companies 的完整内容 原始代码在其脚本中具有以下标记:

<nav id="bottom-panel-pagination" class="panel-pagination hasNextOnly">
    <div data-event="view left" class="prev-page icon-new-left-arrow"></div>
    <div data-event="view right" class="next-page icon-new-right-arrow"></div>
</nav>
Run Code Online (Sandbox Code Playgroud)

我想使用 BeautifulSoup 将类属性“panel-pagination hasNextOnly”更改为“panel-pagination hasNoPagination”。我的 python 代码如下所示:

import urllib2
from bs4 import BeautifulSoup
quote_page = "http://fortune.com/best-companies/"
page = urllib2.urlopen(quote_page)
soup = BeautifulSoup(page, "html.parser")
fullpage = soup.find('nav', attrs = {'class' : 'panel-pagination hasNextOnly'})
print fullpage
Run Code Online (Sandbox Code Playgroud)

我想将 attrs = {'class' : 'panel-pagination hasNextOnly'} 更改为 attrs = {'class' : 'panel-pagination hasNoPagination'}

网站应该在此之后重新加载,以便我可以进一步废弃它。我该怎么做?请帮忙。

python pagination beautifulsoup web-scraping bs4

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

BeautifulSoup.find_all()方法不使用命名空间标记

我今天在使用BeautifulSoup时遇到了一种非常奇怪的行为.

我们来看一个非常简单的html片段:

<html><body><ix:nonfraction>lele</ix:nonfraction></body></html>
Run Code Online (Sandbox Code Playgroud)

我试图<ix:nonfraction>用BeautifulSoup 获取标签的内容.

使用该find方法时一切正常:

from bs4 import BeautifulSoup

html = "<html><body><ix:nonfraction>lele</ix:nonfraction></body></html>"

soup = BeautifulSoup(html, 'lxml') # The parser used here does not matter

soup.find('ix:nonfraction')

>>> <ix:nonfraction>lele</ix:nonfraction>
Run Code Online (Sandbox Code Playgroud)

但是,在尝试使用该find_all方法时,我希望返回一个包含此单个元素的列表,但事实并非如此!

soup.find_all('ix:nonfraction')
>>> []
Run Code Online (Sandbox Code Playgroud)

事实上,find_all每当我正在搜索的标签中出现冒号时,似乎都会返回一个空列表.

我已经能够在两台不同的计算机上重现这个问题.

有没有人有解释,更重要的是,有一个解决方法?我需要使用该find_all方法只是因为我的实际案例要求我在整个html页面上获取所有这些标签.

python beautifulsoup python-3.x bs4

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

抓取要求您向下滚动的网站

我想在这里抓住这个网站:

但是,它需要我向下滚动才能收集其他数据.我不知道如何使用美丽的汤或蟒蛇向下滚动.这里有人知道怎么样吗?

代码有点乱,但现在是.

import scrapy
from scrapy.selector import Selector
from testtest.items import TesttestItem
import datetime
from selenium import webdriver
from bs4 import BeautifulSoup
from HTMLParser import HTMLParser
import re
import time

class MLStripper(HTMLParser):


class MySpider(scrapy.Spider):
        name = "A1Locker"

        def strip_tags(html):
            s = MLStripper()
            s.feed(html)
            return s.get_data()

     allowed_domains = ['https://www.a1lockerrental.com']
    start_urls = ['http://www.a1lockerrental.com/self-storage/mo/st-
 louis/4427-meramec-bottom-rd-facility/unit-sizes-prices#/units?
 category=all']

     def parse(self, response):

                 url='http://www.a1lockerrental.com/self-storage/mo/st-
louis/4427-meramec-bottom-rd-facility/unit-sizes-prices#/units?
category=Small'
                driver = webdriver.Firefox()
                driver.get(url)
                html = driver.page_source
                soup = BeautifulSoup(html, 'html.parser')
        url2='http://www.a1lockerrental.com/self-storage/mo/st-louis/4427-
meramec-bottom-rd-facility/unit-sizes-prices#/units?category=Medium'
        driver2 = webdriver.Firefox()
                driver2.get(url2)
                html2 = driver.page_source …
Run Code Online (Sandbox Code Playgroud)

javascript python dynamic beautifulsoup bs4

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

BS4 select_one vs查找

我想知道表演bs.find('div')和表演有什么区别bs.select_one('div')。这同样适用于find_allselect

在性能上是否存在任何差异,或者在特定情况下是否可以使用其他差异?

python beautifulsoup html-parsing bs4

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

使用python请求和美丽的汤来拉文本

谢谢你看看我的问题.我想知道是否有任何方法可以从这个文本中提取data-sitekey ...这是网页的网址https://e-com.secure.force.com/adidasUSContact/

<div class="g-recaptcha" data-sitekey="6LfI8hoTAAAAAMax5_MTl3N-5bDxVNdQ6Gx6BcKX" data-type="image" id="ncaptchaRecaptchaId"><div style="width: 304px; height: 78px;"><div><iframe src="https://www.google.com/recaptcha/api2/anchor?k=6LfI8hoTAAAAAMax5_MTl3N-5bDxVNdQ6Gx6BcKX&amp;co=aHR0cHM6Ly9lLWNvbS5zZWN1cmUuZm9yY2UuY29tOjQ0Mw..&amp;hl=en&amp;type=image&amp;v=r20160921114513&amp;size=normal&amp;cb=ei2ddcb6rl03" title="recaptcha widget" width="304" height="78" role="presentation" frameborder="0" scrolling="no" name="undefined"></iframe></div><textarea id="g-recaptcha-response" name="g-recaptcha-response" class="g-recaptcha-response" style="width: 250px; height: 40px; border: 1px solid #c1c1c1; margin: 10px 25px; padding: 0px; resize: none;  display: none; "></t
Run Code Online (Sandbox Code Playgroud)

这是我目前的代码

    import requests 
from bs4 import BeautifulSoup

headers = {
    'Host' : 'e-com.secure.force.com',
    'Connection' : 'keep-alive',
    'Upgrade-Insecure-Requests' : '1',
    'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; WOW64)',
    'Accept' : 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
    'Accept-Encoding' : 'gzip, deflate, sdch',
    'Accept-Language' : 'en-US,en;q=0.8'
} …
Run Code Online (Sandbox Code Playgroud)

python beautifulsoup python-requests bs4

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