我想写一段代码,如下所示:
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.
有办法解决以下问题吗?
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
我正在尝试使用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总是非常好的一件事.
我正在使用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']
按预期返回
这两个语句之间的区别是,当要搜索的单词有多个实例时,第二个语句会起作用
import urllib2
website = "WEBSITE"
openwebsite = urllib2.urlopen(website)
html = getwebsite.read()
print html
Run Code Online (Sandbox Code Playgroud)
到现在为止还挺好.
但我只希望纯文本HTML中的href链接.我怎么解决这个问题?
我试图使用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 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&u=12563">port_new_cape</a></td>
<td class="position"><a href="./search.php?id=12563&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 </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) 安装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)
我没有理解为什么它出来以及如何解决它.
如果网页上有<div class="class1">
和<p class="class1">
,然后soup.findAll(True, 'class1')
就会发现他们两个.
<p class="class1 class2">
但是,如果它有,它将无法找到.如何找到具有某个类的所有对象,无论它们是否还有其他类?
我想从网站上删除一个项目列表,并保留它们的显示顺序.这些项目按表格组织,但它们可以是两个不同类别之一(按随机顺序).
有没有办法提供多个类,并让BeautifulSoup4找到任何给定类中的所有项目?
我需要实现此代码的功能,除了保留源代码中的项目顺序:
items = soup.findAll(True,{'class':'class1'})
items += soup.findAll(True,{'class':'class2'})
Run Code Online (Sandbox Code Playgroud) beautifulsoup ×10
python ×10
html ×2
web-scraping ×2
href ×1
html-parsing ×1
hyperlink ×1
mechanize ×1
nltk ×1
urllib3 ×1
user-warning ×1