我一直在玩BeautifulSoup,这很棒.我的最终目标是尝试从页面中获取文本.我只是想从正文文本,用特制的情况下拿到冠军和/或ALT属性从<a>或<img>标签.
到目前为止我有这个EDITED & UPDATED CURRENT CODE:
soup = BeautifulSoup(page)
comments = soup.findAll(text=lambda text:isinstance(text, Comment))
[comment.extract() for comment in comments]
page = ''.join(soup.findAll(text=True))
page = ' '.join(page.split())
print page
Run Code Online (Sandbox Code Playgroud)
1)你有什么建议我的特殊情况的最好方法是不从上面列出的两个标签中排除这些属性?如果它太复杂而不能做到这一点,那就不像做#2那么重要了.
2)我想剥离<!-- -->标签和它们之间的一切.我该怎么办呢?
QUESTION EDIT @jathanism:这里有一些注释标签,我试图去除,但仍然存在,即使我使用你的例子
<!-- Begin function popUp(URL) { day = new Date(); id = day.getTime(); eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=300,height=330,left = 774,top = 518');"); } // End -->
<!-- var MenuBar1 = new …Run Code Online (Sandbox Code Playgroud) 我使用python + BeautifulSoup来解析HTML文档.
现在我需要替换<h2 class="someclass">HTML文档中的所有元素<h1 class="someclass">.
如何在不更改文档中的任何其他内容的情况下更改标记名称?
我制作一个python脚本供个人使用,但它不适用于维基百科......
这项工作:
import urllib2, sys
from bs4 import BeautifulSoup
site = "http://youtube.com"
page = urllib2.urlopen(site)
soup = BeautifulSoup(page)
print soup
Run Code Online (Sandbox Code Playgroud)
这不起作用:
import urllib2, sys
from bs4 import BeautifulSoup
site= "http://en.wikipedia.org/wiki/StackOverflow"
page = urllib2.urlopen(site)
soup = BeautifulSoup(page)
print soup
Run Code Online (Sandbox Code Playgroud)
这是错误:
Traceback (most recent call last):
File "C:\Python27\wiki.py", line 5, in <module>
page = urllib2.urlopen(site)
File "C:\Python27\lib\urllib2.py", line 126, in urlopen
return _opener.open(url, data, timeout)
File "C:\Python27\lib\urllib2.py", line 406, in open
response = meth(req, response)
File "C:\Python27\lib\urllib2.py", line 519, in http_response
'http', …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用漂亮的汤来解析html并找到具有特定锚标记的所有href
<a href="http://example.com">TEXT</a>
<a href="http://example.com/link">TEXT</a>
<a href="http://example.com/page">TEXT</a>
Run Code Online (Sandbox Code Playgroud)
我正在寻找的所有链接都具有完全相同的锚文本,在本例中为TEXT.我不是在寻找TEXT这个词,我想用TEXT这个词找到所有不同的HREF
编辑:
澄清寻找类似于使用类来解析链接的东西
<a href="http://example.com" class="visible">TEXT</a>
<a href="http://example.com/link" class="visible">TEXT</a>
<a href="http://example.com/page" class="visible">TEXT</a>
Run Code Online (Sandbox Code Playgroud)
然后使用
findAll('a', 'visible')
Run Code Online (Sandbox Code Playgroud)
除了我正在解析的HTML没有类但总是相同的锚文本
晚上好,我使用BeautifulSoup从网站中提取一些数据如下:
from BeautifulSoup import BeautifulSoup
from urllib2 import urlopen
soup = BeautifulSoup(urlopen('http://www.fsa.gov.uk/about/media/facts/fines/2002'))
table = soup.findAll('table', attrs={ "class" : "table-horizontal-line"})
print table
Run Code Online (Sandbox Code Playgroud)
这给出了以下输出:
[<table width="70%" class="table-horizontal-line">
<tr>
<th>Amount</th>
<th>Company or person fined</th>
<th>Date</th>
<th>What was the fine for?</th>
<th>Compensation</th>
</tr>
<tr>
<td><a name="_Hlk74714257" id="_Hlk74714257"> </a>£4,000,000</td>
<td><a href="/pages/library/communication/pr/2002/124.shtml">Credit Suisse First Boston International </a></td>
<td>19/12/02</td>
<td>Attempting to mislead the Japanese regulatory and tax authorities</td>
<td> </td>
</tr>
<tr>
<td>£750,000</td>
<td><a href="/pages/library/communication/pr/2002/123.shtml">Royal Bank of Scotland plc</a></td>
<td>17/12/02</td>
<td>Breaches of money laundering rules</td>
<td> </td>
</tr>
<tr>
<td>£1,000,000</td> …Run Code Online (Sandbox Code Playgroud) 我想访问一个名为的标记"name":
<contact><name>Yesügey</name><lastName>Ye?il</lastName><phone>+90 333 9695395</phone></contact>
Run Code Online (Sandbox Code Playgroud)
由于"name"是BeautifulSoup标记对象的属性,我无法访问子标记name:
>>> c1
<contact><name>Yesügey</name><lastname>Ye?il</lastname><phone>+90 333 9695395</p
hone></contact>
>>> c1.name
'contact'
>>> c1.lastname
<lastname>Ye?il</lastname>
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用lxmlBeautifulSoup的解析器,因为默认值较慢,但是我收到此错误:
soup = BeautifulSoup(html, "lxml")
File "/home/rob/python/stock/local/lib/python2.7/site-packages/bs4/__init__.py", line 152, in __init__
% ",".join(features))
bs4.FeatureNotFound: Couldn't find a tree builder with the features you requested: lxml. Do you need to install a parser library?
Run Code Online (Sandbox Code Playgroud)
我已经卸载并重新安装了lxml以及beautifulsoup多次,但它仍然无法读取它.我也尝试重新安装lxml依赖项,我仍然得到这个.
我甚至创建了一个新的虚拟环境并安装了新的一切,但仍然出现此错误.
任何人都知道这里发生了什么?
编辑
在ubuntu桌面上使用Python 2.7.x上的最新版本的bs4和lxml
我可以,import lxml但我不能from lxml import etree回来:
File "<stdin>", line 1, in <module>
ImportError: /usr/lib/x86_64-linux-gnu/libxml2.so.2: version `LIBXML2_2.9.0' not found (required by /home/rob/python/stock/local/lib/python2.7/site-packages/lxml/etree.so)
Run Code Online (Sandbox Code Playgroud)
我有libxml但是我不确定版本,但我安装并重新安装了最新版本.还尝试手动安装2.9.0仍然没有
我试图刮掉http://www.dailyfinance.com/quote/NYSE/international-business-machines/IBM/financial-ratios,但传统的网址字符串构建技术不起作用,因为"全公司名称-is-inserted-in-path"字符串.并且事先并不知道确切的"完整公司名称".只有公司的标志,"IBM"才知道.
从本质上讲,我刮擦的方式是循环遍历公司符号数组并在将url字符串发送到urllib2.urlopen(url)之前构建它.但在这种情况下,这是不可能做到的.
例如,CSCO字符串是
http://www.dailyfinance.com/quote/NASDAQ/cisco-systems-inc/CSCO/financial-ratios
Run Code Online (Sandbox Code Playgroud)
另一个例子url字符串是AAPL:
http://www.dailyfinance.com/quote/NASDAQ/apple/AAPL/financial-ratios
Run Code Online (Sandbox Code Playgroud)
因此,为了获取URL,我必须在主页的输入框中搜索符号:
http://www.dailyfinance.com/
Run Code Online (Sandbox Code Playgroud)
我注意到当我输入"CSCO"并在Firefox Web开发人员网络选项卡中的(http://www.dailyfinance.com/quote/NASDAQ/apple/AAPL/financial-ratios)检查搜索输入时,我注意到获取请求正在发送给
http://j.foolcdn.com/tmf/predictivesearch?callback=_predictiveSearch_csco&term=csco&domain=dailyfinance.com
Run Code Online (Sandbox Code Playgroud)
并且引用者实际上给出了我想要捕获的路径
Host: j.foolcdn.com
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:28.0) Gecko/20100101 Firefox/28.0
Accept: */*
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Referer: http://www.dailyfinance.com/quote/NASDAQ/cisco-systems-inc/CSCO/financial-ratios?source=itxwebtxt0000007
Connection: keep-alive
Run Code Online (Sandbox Code Playgroud)
很抱歉很长的解释.所以问题是如何在Referer中提取网址?如果那是不可能的,我应该如何处理这个问题?还有另外一种方法吗?
我非常感谢你的帮助.
我用BeautifulSoup做了这个,但它有点麻烦,我想弄清楚我是否可以直接用Selenium来做.
假设我有以下HTML,它在页面源中重复多次,具有相同的元素但内容不同:
<div class="person">
<div class="title">
<a href="http://www.url.com/johnsmith/">John Smith</a>
</div>
<div class="company">
<a href="http://www.url.com/company/">SalesForce</a>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
我需要建立一个字典,每个人的条目看起来像:
dict = {'name' : 'John Smith', 'company' : 'SalesForce'}
Run Code Online (Sandbox Code Playgroud)
通过执行以下操作,我可以轻松地让Selenium生成每个顶级元素的内容列表:
driver.find_elements_by_class_name('person')
Run Code Online (Sandbox Code Playgroud)
但是后来我不能遍历列表,因为上面的方法不会将范围/源缩小到只是该元素的内容.
如果我尝试做这样的事情:
people = driver.find_elements_by_class_name('person')
for person in people:
print person.find_element_by_xpath['//div[@class="title"]//a').text
Run Code Online (Sandbox Code Playgroud)
我一遍又一遍地得到同样的名字.
我需要按组进行分组,因为在我的情况下,遍历整个页面并单独附加每个标记将无效(存在无限滚动,因此效率非常低).
有谁知道是否有可能直接在Selenium中这样做,如果是这样的话怎么样?
我一直在玩美丽的汤和解析网页几天.我一直在使用一行代码,这些代码在我编写的所有脚本中都是我的救星.代码行是:
r = requests.get('some_url', auth=('my_username', 'my_password')).
Run Code Online (Sandbox Code Playgroud)
但......
我想用(打开一个带有身份验证的URL)做同样的事情:
(1) sauce = urllib.request.urlopen(url).read() (1)
(2) soup = bs.BeautifulSoup(sauce,"html.parser") (2)
Run Code Online (Sandbox Code Playgroud)
我无法打开网址并阅读需要身份验证的网页.我如何实现这样的目标:
(3) sauce = urllib.request.urlopen(url, auth=(username, password)).read() (3)
instead of (1)
Run Code Online (Sandbox Code Playgroud) beautifulsoup ×10
python ×10
html-parsing ×2
python-2.7 ×2
csv ×1
html ×1
lxml ×1
python-3.x ×1
request ×1
selenium ×1
url ×1
urllib2 ×1
web-scraping ×1
xml ×1