标签: beautifulsoup

在urllib3中我应该使用什么来打开url而不是urlopen

我想写一段代码,如下所示:

from bs4 import BeautifulSoup
import urllib2

url = 'http://www.thefamouspeople.com/singers.php'
html = urllib2.urlopen(url)
soup = BeautifulSoup(html)
Run Code Online (Sandbox Code Playgroud)

但我发现我现在必须安装urllib3包.

此外,我找不到任何教程或示例来了解如何重写上面的代码,例如,urllib3没有urlopen.

请问任何解释或示例?!

P/S:我正在使用python 3.4.

python beautifulsoup web-scraping urllib3

46
推荐指数
3
解决办法
10万
查看次数

屏幕抓取:绕过"HTTP错误403:robots.txt禁止请求"

有办法解决以下问题吗?

httperror_seek_wrapper: HTTP Error 403: request disallowed by robots.txt
Run Code Online (Sandbox Code Playgroud)

是唯一的方法来联系网站所有者(barnesandnoble.com)..我正在建立一个网站,将带来更多的销售,不知道为什么他们会拒绝在一定深度访问.

我在Python2.6上使用了mechanize和BeautifulSoup.

希望能够解决问题

python screen-scraping mechanize beautifulsoup http-status-code-403

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

BeatifulSoup4 get_text仍然有javascript

我正在尝试使用bs4删除所有的html/javascript,但是,它并没有摆脱javascript.我仍然在那里看到它的文字.我怎么能绕过这个?

我试着用nltk然而,工作正常,clean_html并且clean_url将被删除向前发展.有没有办法使用汤get_text并获得相同的结果?

我试着看看这些其他页面:

BeautifulSoup get_text不会删除所有标记和JavaScript

目前我正在使用nltk已弃用的功能.

编辑

这是一个例子:

import urllib
from bs4 import BeautifulSoup

url = "http://www.cnn.com"
html = urllib.urlopen(url).read()
soup = BeautifulSoup(html)
print soup.get_text()
Run Code Online (Sandbox Code Playgroud)

我仍然看到CNN的以下内容:

$j(function() {
"use strict";
if ( window.hasOwnProperty('safaripushLib') && window.safaripushLib.checkEnv() ) {
var pushLib = window.safaripushLib,
current = pushLib.currentPermissions();
if (current === "default") {
pushLib.checkPermissions("helloClient", function() {});
}
}
});

/*globals MainLocalObj*/
$j(window).load(function () {
'use strict';
MainLocalObj.init();
});
Run Code Online (Sandbox Code Playgroud)

我怎样才能删除js?

我找到的其他选项是:

https://github.com/aaronsw/html2text

问题html2text在于它有时真的慢,并且会产生明显的滞后,这是nltk总是非常好的一件事.

python beautifulsoup nltk

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

使用BeautifulSoup在html中搜索字符串

我正在使用BeautifulSoup在特定页面上查找用户输入的字符串.例如,我想看看字符串'Python'是否位于页面上:http://python.org

当我使用: find_string = soup.body.findAll(text='Python') find_string返回[]

但是当我使用: find_string = soup.body.findAll(text=re.compile('Python'), limit=1) find_string [u'Python Jobs']按预期返回

这两个语句之间的区别是,当要搜索的单词有多个实例时,第二个语句会起作用

python beautifulsoup

43
推荐指数
3
解决办法
8万
查看次数

如何使用Python从HTML获取href链接?

import urllib2

website = "WEBSITE"
openwebsite = urllib2.urlopen(website)
html = getwebsite.read()

print html
Run Code Online (Sandbox Code Playgroud)

到现在为止还挺好.

但我只希望纯文本HTML中的href链接.我怎么解决这个问题?

html python beautifulsoup href hyperlink

41
推荐指数
8
解决办法
12万
查看次数

使用Python将html转换为文本

我试图使用Python将html块转换为文本.

输入:

<div class="body"><p><strong></strong></p>
<p><strong></strong>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa</p>
<p>Consectetuer adipiscing elit. <a href="http://example.com/" target="_blank" class="source">Some Link</a> Aenean commodo ligula eget dolor. Aenean massa</p>
<p>Aenean massa.Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa</p>
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa</p>
<p>Consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa</p></div>
Run Code Online (Sandbox Code Playgroud)

期望的输出:

Lorem ipsum dolor坐在amet,consectetuer adipiscing …

python beautifulsoup web-scraping

41
推荐指数
6
解决办法
6万
查看次数

使用Python和BeautifulSoup(将保存的网页源代码保存到本地文件中)

我使用的是Python 2.7 + BeautifulSoup 4.3.2.

我正在尝试使用Python和BeautifulSoup来获取网页上的信息.由于网页在公司网站上需要登录和重定向,因此我将目标网页的源代码复制到文件中,并将其保存为C:\中的"example.html",以方便练习.

这是原始代码的一部分:

<tr class="ghj">
    <td><span class="city-sh"><sh src="./citys/1.jpg" alt="boy" title="boy" /></span><a href="./membercity.php?mode=view&amp;u=12563">port_new_cape</a></td>
    <td class="position"><a href="./search.php?id=12563&amp;sr=positions" title="Search positions">452</a></td>
    <td class="details"><div>South</div></td>
    <td>May 09, 1997</td>
    <td>Jan 23, 2009 12:05 pm&nbsp;</td>
</tr>
Run Code Online (Sandbox Code Playgroud)

到目前为止我编写的代码是:

from bs4 import BeautifulSoup
import re
import urllib2

url = "C:\example.html"
page = urllib2.urlopen(url)
soup = BeautifulSoup(page.read())

cities = soup.find_all('span', {'class' : 'city-sh'})

for city in cities:
print city
Run Code Online (Sandbox Code Playgroud)

这只是测试的第一阶段,有些没有完成.

但是,当我运行它时,它会显示错误消息,使用"urllib2.urlopen"来打开本地文件似乎是不合适的.

 Traceback (most recent call last):
   File "C:\Python27\Testing.py", line 8, in <module>
     page = urllib2.urlopen(url)
   File "C:\Python27\lib\urllib2.py", …
Run Code Online (Sandbox Code Playgroud)

python beautifulsoup

41
推荐指数
3
解决办法
7万
查看次数

如何摆脱BeautifulSoup用户警告?

安装BeautifulSoup之后,每当我在cmd中运行我的Python时,就会出现这个警告.

D:\Application\python\lib\site-packages\beautifulsoup4-4.4.1-py3.4.egg\bs4\__init__.py:166:
UserWarning: No parser was explicitly specified, so I'm using the best
available HTML parser for this system ("html.parser"). This usually isn't a
problem, but if you run this code on another system, or in a different
virtual environment, it may use a different parser and behave differently.

To get rid of this warning, change this:

 BeautifulSoup([your markup])

to this:

 BeautifulSoup([your markup], "html.parser")
Run Code Online (Sandbox Code Playgroud)

我没有理解为什么它出来以及如何解决它.

python beautifulsoup user-warning

41
推荐指数
4
解决办法
5万
查看次数

如果对象也有其他类,Beautiful Soup也找不到CSS类

如果网页上有<div class="class1"><p class="class1">,然后soup.findAll(True, 'class1')就会发现他们两个.

<p class="class1 class2">但是,如果它有,它将无法找到.如何找到具有某个类的所有对象,无论它们是否还有其他类?

python screen-scraping beautifulsoup

40
推荐指数
3
解决办法
1万
查看次数

BeautifulSoup findAll()给出了多个类?

我想从网站上删除一个项目列表,并保留它们的显示顺序.这些项目按表格组织,但它们可以是两个不同类别之一(按随机顺序).

有没有办法提供多个类,并让BeautifulSoup4找到任何给定类中的所有项目?

我需要实现此代码的功能,除了保留源代码中的项目顺序:

items = soup.findAll(True,{'class':'class1'})
items += soup.findAll(True,{'class':'class2'})
Run Code Online (Sandbox Code Playgroud)

html python beautifulsoup html-parsing

40
推荐指数
5
解决办法
5万
查看次数