标签: beautifulsoup

Selenium与BeautifulSoup用于网络抓取

我正在使用Python从网站上抓取内容.首先我使用BeautifulSoupMechanize在Python上,但我看到该网站有一个按钮,通过JavaScript创建内容所以我决定使用Selenium.

鉴于我可以使用Selenium找到元素并使用类似方法获取其内容driver.find_element_by_xpath,BeautifulSoup当我可以将Selenium用于所有内容时,有什么理由可以使用?

在这种特殊情况下,我需要使用Selenium来点击JavaScript按钮,因此最好使用Selenium进行解析,还是应该同时使用Selenium和Beautiful Soup?

javascript python selenium beautifulsoup

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

无法安装包美丽的汤.错误消息是"SyntaxError:调用'print'时缺少括号"

我在Windows 8计算机上安装了Python 3.5.我还安装了Pycharm Community Version 5.0.4.我无法通过Pycharm中的设置选项安装BeautifulSoup模块.我在Pycharm中收到以下错误:

Collecting BeautifulSoup
  Using cached BeautifulSoup-3.2.1.tar.gz
  Complete output from command python setup.py egg_info:
  Traceback (most recent call last):
    File "<string>", line 1, in <module>
    File "C:\Users\Kashyap\AppData\Local\Temp\pycharm-packaging0.tmp\BeautifulSoup\setup.py", line 22
      print "Unit tests have failed!"
                                    ^
  SyntaxError: Missing parentheses in call to 'print'

  ----------------------------------------

Command "python setup.py egg_info" failed with error code 1 in C:\Users\Kashyap\AppData\Local\Temp\pycharm-packaging0.tmp\BeautifulSoup
Run Code Online (Sandbox Code Playgroud)

安装的Python文件夹的路径是3.5.1(C:\ Program Files(x86)\ Python35-32\python.exe)

python beautifulsoup python-3.x

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

Python BeautifulSoup提取元素之间的文本

我尝试从以下HTML中提取"这是我的文本":

<html>
<body>
<table>
   <td class="MYCLASS">
      <!-- a comment -->
      <a hef="xy">Text</a>
      <p>something</p>
      THIS IS MY TEXT
      <p>something else</p>
      </br>
   </td>
</table>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

我这样试过:

soup = BeautifulSoup(html)

for hit in soup.findAll(attrs={'class' : 'MYCLASS'}):
    print hit.text
Run Code Online (Sandbox Code Playgroud)

但我得到所有嵌套标签和评论之间的所有文本.

任何人都可以帮助我从中获得"这是我的文字"吗?

python beautifulsoup

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

BeautifulSoup和lxml.html - 更喜欢什么?

我正在开发一个涉及解析HTML的项目.

搜索后,我发现了两个可能的选项:BeautifulSoup和lxml.html

有什么理由比较喜欢一个吗?我已经在一段时间后使用了lxml for XML,我觉得我会更舒服,但是BeautifulSoup似乎很常见.

我知道我应该使用适合我的那个,但我正在寻找两者的个人经历.

python lxml beautifulsoup

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

BeautifulSoup:从锚标记中提取文本

我想提取:

  • 来自image标签的src的文本和
  • div类标记内的锚标记的文本

我成功地设法提取img src,但是无法从锚标记中提取文本.

<a class="title" href="http://www.amazon.com/Nikon-COOLPIX-Digital-Camera-NIKKOR/dp/B0073HSK0K/ref=sr_1_1?s=electronics&amp;ie=UTF8&amp;qid=1343628292&amp;sr=1-1&amp;keywords=digital+camera">Nikon COOLPIX L26 16.1 MP Digital Camera with 5x Zoom NIKKOR Glass Lens and 3-inch LCD (Red)</a> 
Run Code Online (Sandbox Code Playgroud)

这是整个HTML页面的链接.

这是我的代码:

for div in soup.findAll('div', attrs={'class':'image'}):
    print "\n"
    for data in div.findNextSibling('div', attrs={'class':'data'}):
        for a in data.findAll('a', attrs={'class':'title'}):
            print a.text
    for img in div.findAll('img'):
        print img['src']
Run Code Online (Sandbox Code Playgroud)

我想要做的是提取图像src(链接)和里面的标题div class=data,例如:

 <a class="title" href="http://www.amazon.com/Nikon-COOLPIX-Digital-Camera-NIKKOR/dp/B0073HSK0K/ref=sr_1_1?s=electronics&amp;ie=UTF8&amp;qid=1343628292&amp;sr=1-1&amp;keywords=digital+camera">Nikon COOLPIX L26 16.1 MP Digital Camera with 5x Zoom NIKKOR Glass Lens and 3-inch LCD (Red)</a> …
Run Code Online (Sandbox Code Playgroud)

html python tags beautifulsoup scraper

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

将</ br>转换为结束行

我正在尝试使用提取一些文本BeautifulSoup.我正在get_text()为此目的使用功能.

我的问题是文本包含</br>标签,我需要将它们转换为结束行.我怎样才能做到这一点?

beautifulsoup

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

BeautifulSoup - 在标签内按文字搜索

请注意以下问题:

import re
from bs4 import BeautifulSoup as BS

soup = BS("""
<a href="/customer-menu/1/accounts/1/update">
    Edit
</a>
""")

# This returns the <a> element
soup.find(
    'a',
    href="/customer-menu/1/accounts/1/update",
    text=re.compile(".*Edit.*")
)

soup = BS("""
<a href="/customer-menu/1/accounts/1/update">
    <i class="fa fa-edit"></i> Edit
</a>
""")

# This returns None
soup.find(
    'a',
    href="/customer-menu/1/accounts/1/update",
    text=re.compile(".*Edit.*")
)
Run Code Online (Sandbox Code Playgroud)

出于某种原因,当<i>标签存在时,BeautifulSoup将不匹配文本.查找标签并显示其文本

>>> a2 = soup.find(
        'a',
        href="/customer-menu/1/accounts/1/update"
    )
>>> print(repr(a2.text))
'\n Edit\n'
Run Code Online (Sandbox Code Playgroud)

对.根据Docs,汤使用正则表达式的匹配函数,而不是搜索函数.所以我需要提供DOTALL标志:

pattern = re.compile('.*Edit.*')
pattern.match('\n Edit\n')  # Returns None

pattern = re.compile('.*Edit.*', flags=re.DOTALL)
pattern.match('\n Edit\n')  # …
Run Code Online (Sandbox Code Playgroud)

python regex beautifulsoup

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

初学者通过Python学习屏幕抓取的最佳方式

这可能是难以回答的问题之一,但这里有:

我不认为我的自编程员 - 但我想:-)我已经学会了R,因为我厌倦了spss,而且因为一位朋友向我介绍了这种语言 - 所以我不是一个完全陌生的人编程逻辑.

现在我想学习python - 主要是做屏幕抓取和文本分析,还用于用Pylons或Django编写webapps.

那么:我应该如何学习使用python进行屏幕刮擦?我开始经历那些杂乱无章的文档,但我觉得很多"魔术"正在进行 - 毕竟 - 我正在努力学习,而不仅仅是做.

另一方面:没有理由重新发明轮子,如果Scrapy要屏蔽Django对网页的影响,那么毕竟值得直接进入Scrapy.你怎么看?

哦 - 顺便说一句:屏幕抓取的那种:我想要报道网站(即相当复杂和大的)来提及政治家等等 - 这意味着我需要每天,递增和递归地刮 - 我需要记录结果进入各种各样的数据库 - 这引出了一个奖励问题:每个人都在谈论非SQL数据库.我是否应该立即学会使用例如mongoDB(我认为我不需要强烈的一致性),或者我想做什么是愚蠢的?

感谢您的任何想法 - 如果这是一般被认为是一个编程问题,我道歉.

python lxml screen-scraping beautifulsoup scrapy

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

美丽的汤找到特殊div的孩子

我试图用Python-> Beautiful Soup解析一个看起来像这样的网页:在此输入图像描述

我试图提取突出显示的td div的内容.目前我可以得到所有的div

alltd = soup.findAll('td')


for td in alltd:
    print td
Run Code Online (Sandbox Code Playgroud)

但是我试图缩小范围以搜索"tablebox"类中的tds,它仍然可能会返回30+但是更容易管理的数字超过300+.

如何提取上图中突出显示的td的内容?

python parsing beautifulsoup

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

BeautifulSoup中是否有一个相同的InnerText?

使用以下代码:

soup = BeautifulSoup(page.read(), fromEncoding="utf-8")
result = soup.find('div', {'class' :'flagPageTitle'})
Run Code Online (Sandbox Code Playgroud)

我得到以下html:

<div id="ctl00_ContentPlaceHolder1_Item65404" class="flagPageTitle" style=" ">
<span></span><p>Some text here</p>
</div>
Run Code Online (Sandbox Code Playgroud)

如何在Some text here没有任何标签的情况下获得?是否有InnerText等效BeautifulSoup

python beautifulsoup

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