什么是抓住动态网站的最佳方法,其中大部分内容是由似乎是ajax请求生成的?我之前有过使用Mechanize,BeautifulSoup和python组合的经验,但我还有新的东西.
- 编辑 - 更多细节:我正在试图刮掉CNN 主数据库.那里有大量的信息,但似乎没有api.
我正在编写一个用于编辑XML文件的脚本BeautifulStoneSoup,但该库会将所有标记转换为小写.是否可以选择保存案例?
import BeautifulSoup
xml = "<TestTag>a string</TestTag>"
soup = BeautifulSoup.BeautifulStoneSoup(xml, markupMassage=False)
print soup.prettify() # or soup.renderContents()
#prints
>>> <testtag>a string</testtag>
#instead of the expected
>>> <TestTag>a string</TestTag>
Run Code Online (Sandbox Code Playgroud) 我正在使用BeautifulSoup和Requests抓取一些网站.我正在检查的页面中有一个数据位于<script language="JavaScript" type="text/javascript">标记内.它看起来像这样:
<script language="JavaScript" type="text/javascript">
var page_data = {
"default_sku" : "SKU12345",
"get_together" : {
"imageLargeURL" : "http://null.null/pictures/large.jpg",
"URL" : "http://null.null/index.tmpl",
"name" : "Paints",
"description" : "Here is a description and it works pretty well",
"canFavorite" : 1,
"id" : 1234,
"type" : 2,
"category" : "faded",
"imageThumbnailURL" : "http://null.null/small9.jpg"
......
Run Code Online (Sandbox Code Playgroud)
有没有办法可以page_data在此脚本标记中的变量中创建python字典或json对象?那会比使用BeautifulSoup获得价值更好.
有没有类似的库来BeautifulSoup进行C#?
我想简单地解析HTML和XML,特别是有错误的HTML.
我想做以下代码,这是BS文档要做的,唯一的问题是"类"这个词不仅仅是一个单词.它可以在HTML中找到,但它也是一个python关键字,导致此代码抛出错误.
那么我该怎么做呢?
soup.findAll('ul', class="score")
Run Code Online (Sandbox Code Playgroud) 我正在尝试解析标记之间的文本<blockquote>.当我输入soup.blockquote.get_text().
我得到了我想要的HTML文件中第一个出现的blockquote的结果.如何<blockquote>在文件中找到下一个和顺序标记?也许我只是累了,在文档中找不到它.
示例HTML文件:
<html>
<head>header
</head>
<blockquote>I can get this text
</blockquote>
<p>eiaoiefj</p>
<blockquote>trying to capture this next
</blockquote>
<p></p><strong>do not capture this</strong>
<blockquote>
capture this too but separately after "capture this next"
</blockquote>
</html>
Run Code Online (Sandbox Code Playgroud)
简单的python代码:
from bs4 import BeautifulSoup
html_doc = open("example.html")
soup = BeautifulSoup(html_doc)
print.(soup.blockquote.get_text())
# how to get the next blockquote???
Run Code Online (Sandbox Code Playgroud) 我想尝试制作一个从互联网上下载图像的程序,我找到了一个使用美味汤的指南.我之前听说过美丽的汤,所以我想我会尝试一下.我唯一的问题是我似乎找不到Python 3的版本.我去了他们的网站,但我找不到适用于Python 3的版本.
每当我运行setup.py文件时,我都会收到一个错误太快的错误,但它看起来像是在说语法错误.所以我查看了代码并意识到前面或后面没有任何括号应该打印.
我尝试了很多不同的网页和不同的搜索,但无法找到答案.
如果这不是与编程相关的问题,我也很抱歉,如果不是,请对此问题发表评论,我会尽快删除这个问题.
我正在用美味的汤.有这样的标签:
<li><a href="example"> s.r.o., <small>small</small></a></li>
我想获得不在<a>标签中的文字.所以我想把" sro "作为输出.
我试过<small>但它不起作用.BS4中有命令可以做到吗?
谢谢
我需要使我的代码向后兼容python2.6和BeautifulSoup 3.我的代码是使用python2.7编写的,在这种情况下使用BS4.但是当我尝试在squeezy服务器上运行它时,我得到了这个错误(它有python2.6和bs3):
try:
from bs4 import BeautifulSoup
except ImportError:
from BeautifulSoup import BeautifulSoup
gmp = open(fname, 'r')
soup = BeautifulSoup(gmp)
p = soup.body.div.find_all('p')
p = soup.body.div.find_all('p')
TypeError: 'NoneType' object is not callable
Run Code Online (Sandbox Code Playgroud)
如果我改为:
p = soup.body.div.findAll('p')
Run Code Online (Sandbox Code Playgroud)
然后我收到这个错误:
p = soup.body.div.findAll('p')
TypeError: 'NoneType' object is not callable
Run Code Online (Sandbox Code Playgroud)
抛出错误的更新
File "/home/user/openerp/7.0/addons/my_module/models/gec.py", line 401, in parse_html_data
p = soup.body.div.findAll('p') #used findAll instead of find_all for backwards compatability to bs3 version
TypeError: 'NoneType' object is not callable
Run Code Online (Sandbox Code Playgroud)
无论哪种方式,这两种方法都可以在我的Ubuntu上运行python2.7和bs4,但不能用于squeezy.那些我没有看到/知道的版本之间是否有任何其他差异并给我这个错误?
在BeautifulSoup,.text和之间有什么区别.get_text()吗?
获取元素的文本应该首选哪一个?
>>> from bs4 import BeautifulSoup
>>>
>>> html = "<div>text1 <span>text2</span><div>"
>>> soup = BeautifulSoup(html, "html.parser")
>>> div = soup.div
>>> div.text
'text1 text2'
>>> div.get_text()
'text1 text2'
Run Code Online (Sandbox Code Playgroud) beautifulsoup ×10
python ×10
html ×4
html-parsing ×2
ajax ×1
c# ×1
json ×1
parsing ×1
python-2.7 ×1
web-scraping ×1
xml ×1