标签: beautifulsoup

应用引擎上的python lxml?

我可以在谷歌应用引擎上使用python lxml吗?(或者我必须使用美丽的汤?)

我已经开始使用Beautiful Soup但它看起来很慢.我刚刚开始使用来自其他网站的"屏幕抓取"数据的想法来创建某种"混搭".

python google-app-engine lxml beautifulsoup

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

如何制作美丽的汤输出HTML实体?

我正在尝试清理和XSS证明来自客户端的一些HTML输入.我正在使用Python 2.6和Beautiful Soup.我解析输入,剥离不在白名单中的所有标签和属性,并将树转换回字符串.

然而...

>>> unicode(BeautifulSoup('text < text'))
u'text < text'
Run Code Online (Sandbox Code Playgroud)

对我来说,这看起来不像是有效的HTML.使用我的标签剥离器,它打开了各种各样的肮脏的方式:

>>> print BeautifulSoup('<<script></script>script>alert("xss")<<script></script>script>').prettify()
<
<script>
</script>
script>alert("xss")<
<script>
</script>
script>
Run Code Online (Sandbox Code Playgroud)

这些<script></script>对将被删除,剩下的不仅是XSS攻击,甚至是有效的HTML.

The obvious solution is to replace all < characters by &lt; that, after parsing, are found not to belong to a tag (and similar for >&'"). But the Beautiful Soup documentation only mentions the parsing of entities, not the producing of them. Of course I can run a replace over all NavigableString nodes, but …

html python xss beautifulsoup

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

一个快速的python HTML解析器

我写了一个python脚本来处理大量下载的网页HTML(120K页).我需要解析它们并从那里提取一些信息.我尝试使用BeautifulSoup,这很容易和直观,但似乎运行速度非常慢.因为这是必须经常在弱机器上运行的事情(在亚马逊上),速度很重要.python中是否有一个HTML/XML解析器,它的工作速度比BeautifulSoup快得多?或者我必须采用正则表达式解析..

html python xml beautifulsoup

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

使用BeautifulSoup从表中提取选定的列

我试图使用BeautifulSoup 提取此数据表的第一列和第三列.通过查看HTML,第一列有一个<th>标记.另一个感兴趣的列有<td>标记.在任何情况下,我所能得到的只是带有标签的列的列表.但是,我只想要文本.

table已经是一个列表,所以我不能使用findAll(text=True).我不知道如何以另一种形式获得第一列的列表.

from BeautifulSoup import BeautifulSoup
from sys import argv
import re

filename = argv[1] #get HTML file as a string
html_doc = ''.join(open(filename,'r').readlines())
soup = BeautifulSoup(html_doc)
table = soup.findAll('table')[0].tbody.th.findAll('th') #The relevant table is the first one

print table
Run Code Online (Sandbox Code Playgroud)

python beautifulsoup html-parsing

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

如何使用美丽的汤和重新找到包含特定文本的特定类的跨度?

如何找到'blue'包含以下格式的文本类的所有span :

04/18/13 7:29pm
Run Code Online (Sandbox Code Playgroud)

因此可能是:

04/18/13 7:29pm
Run Code Online (Sandbox Code Playgroud)

要么:

Posted on 04/18/13 7:29pm
Run Code Online (Sandbox Code Playgroud)

在构建执行此操作的逻辑方面,这是我到目前为止所得到的:

new_content = original_content.find_all('span', {'class' : 'blue'}) # using beautiful soup's find_all
pattern = re.compile('<span class=\"blue\">[data in the format 04/18/13 7:29pm]</span>') # using re
for _ in new_content:
    result = re.findall(pattern, _)
    print result
Run Code Online (Sandbox Code Playgroud)

我一直指的是/sf/answers/541297921//sf/answers/856039411/试图找到一种方法来做到这一点,但以上就是我到目前为止所有的.

编辑:

为了澄清这个场景,有以下几点:

<span class="blue">here is a lot of text that i don't need</span>
Run Code Online (Sandbox Code Playgroud)

<span class="blue">this is the span i need because it contains 04/18/13 7:29pm</span>
Run Code Online (Sandbox Code Playgroud)

并注意我只需要 …

python regex beautifulsoup

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

来自os.mkdir的"没有这样的文件或目录"

在python项目上工作,它的作用是查看lifehacker.com的索引​​,然后查找所有带有"headline h5 hover-highlight entry-title"类的标签,然后为每个目录创建文件.但唯一的问题是,当我运行它,我得到OSError: [Errno 2] No such file or directory: "/home/root/python/The Sony Smartwatch 3: A Runner's Perspective (Updated: 1/5/2015)"

帮助会很可爱,谢谢!

继承我的代码atm:

import re
import os
import urllib2
from bs4 import BeautifulSoup
from mechanize import Browser

url = "http://lifehacker.com/"
url_open = urllib2.urlopen(url)
soup = BeautifulSoup(url_open.read())

link = soup.findAll("h1",{"class": "headline h5 hover-highlight entry-title"})
file_directory = "/home/root/python/"

for i in link:
    os.mkdir(os.path.join(file_directory, str(i.text)))
    print "Successfully made directory(s)", i.text, "!"
else:
    print "The directory", i.text, "either exists, or there was an error!"
Run Code Online (Sandbox Code Playgroud)

python beautifulsoup

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

如何用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万
查看次数

python中[:]的含义是什么?

该行在del taglist[:]下面的代码中做了什么?

import urllib
from bs4 import BeautifulSoup
taglist=list()
url=raw_input("Enter URL: ")
count=int(raw_input("Enter count:"))
position=int(raw_input("Enter position:"))
for i in range(count):
    print "Retrieving:",url
    html=urllib.urlopen(url).read()
    soup=BeautifulSoup(html)
    tags=soup('a')
    for tag in tags:
        taglist.append(tag)
    url = taglist[position-1].get('href', None)
    del taglist[:]
print "Retrieving:",url
Run Code Online (Sandbox Code Playgroud)

问题是"编写一个扩展到http://www.pythonlearn.com/code/urllinks.py的Python程序.该程序将使用urllib从下面的数据文件中读取HTML,从锚点中提取href = vaues标签,扫描相对于列表中第一个名称的特定位置的标签,按照该链接重复该过程多次并报告您找到的姓氏".示例问题:从http://python-data.dr-chuck.net/known_by_Fikret.html开始 查找位置3的链接(名字是1).请关注该链接.重复此过程4次.答案是您检索的姓氏.姓名序列:Fikret Montgomery Mhairade Butchi Anayah姓氏顺序:Anayah

python beautifulsoup web-scraping python-2.7

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

无法用尽我的刮刀中使用的所有相同网址的内容

我在python中使用BeautifulSoup库编写了一个刮刀来解析遍历网站不同页面的所有名称.如果不是一个不同分页的网址,我可以管理它,这意味着一些网址有一些不分页,因为内容很少.

我的问题是:我怎么能设法在一个函数中编译它们来处理它们是否有分页?

我最初的尝试(它只能解析每个网址首页的内容):

import requests 
from bs4 import BeautifulSoup

urls = {
    'https://www.mobilehome.net/mobile-home-park-directory/maine/all',
    'https://www.mobilehome.net/mobile-home-park-directory/rhode-island/all',
    'https://www.mobilehome.net/mobile-home-park-directory/new-hampshire/all',
    'https://www.mobilehome.net/mobile-home-park-directory/vermont/all'
}

def get_names(link):
    r = requests.get(link)
    soup = BeautifulSoup(r.text,"lxml")
    for items in soup.select("td[class='table-row-price']"):
        name = items.select_one("h2 a").text
        print(name)

if __name__ == '__main__':
    for url in urls:
        get_names(url)
Run Code Online (Sandbox Code Playgroud)

如果有一个像下面这样的分页的网址,我本可以设法做到这一切:

from bs4 import BeautifulSoup 
import requests

page_no = 0
page_link = "https://www.mobilehome.net/mobile-home-park-directory/new-hampshire/all/page/{}"

while True:
    page_no+=1
    res = requests.get(page_link.format(page_no))
    soup = BeautifulSoup(res.text,'lxml')
    container = soup.select("td[class='table-row-price']")
    if len(container)<=1:break 

    for content in container:
        title = content.select_one("h2 a").text
        print(title)
Run Code Online (Sandbox Code Playgroud)

但是,所有网址都没有分页.那么,无论是否有任何分页,我怎么能设法抓住所有这些?

python beautifulsoup web-scraping python-3.x

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

无法从具有不同深度的某些链接中解析产品名称

我在python中编写了一个脚本来到达目标页面,其中每个类别在网站中都有可用的项目名称.我的下面的脚本可以从大多数链接获取产品名称(通过流动类别链接生成,然后是子类别链接).

该脚本可以解析在点击+下面图像中可见的每个类别旁边的符号时显示的子类别链接,然后解析目标页面中的所有产品名称.这是此类目标页面之一.

但是,很少链接与其他链接的深度不同.例如此链接这一个是来自像往常不同环节这一块.

如何从所有链接获取所有产品名称,无论其深度如何?

这是我到目前为止所尝试的:

import requests
from urllib.parse import urljoin
from bs4 import BeautifulSoup

link = "https://www.courts.com.sg/"

res = requests.get(link)
soup = BeautifulSoup(res.text,"lxml")
for item in soup.select(".nav-dropdown li a"):
    if "#" in item.get("href"):continue  #kick out invalid links
    newlink = urljoin(link,item.get("href"))
    req = requests.get(newlink)
    sauce = BeautifulSoup(req.text,"lxml")
    for elem in sauce.select(".product-item-info .product-item-link"):
        print(elem.get_text(strip=True))
Run Code Online (Sandbox Code Playgroud)

如何找到trget链接:

在此输入图像描述

python beautifulsoup web-scraping python-3.x

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