我知道用beautifulsoup编辑文本的能力,是否可以编辑href链接?我希望能够说出<a href="/foo/bar/">并使用beautifulsoup来改变它<a href="http://www.foobarinc.com/foo/bar/">.我不确定如何使用beautifulsoup来做到这一点?任何帮助,非常感谢.
我有一个文件夹的Web索引视图...
<ul><li><a href="/sustainabilitymedia/pics/s5/"> Parent Directory</a></li>
<li><a href="n150850_.jpg"> n150850_.jpg</a></li>
<li><a href="n150850_ss.jpg"> n150850_ss.jpg</a></li>
<li><a href="n150850q.jpg"> n150850q.jpg</a></li>
<li><a href="n150858_.jpg"> n150858_.jpg</a></li>
<li><a href="n150858_ss.jpg"> n150858_ss.jpg</a></li>
<li><a href="n150858q.jpg"> n150858q.jpg</a></li>
<li><a href="n150906_.jpg"> n150906_.jpg</a></li>
<li><a href="n150906_ss.jpg"> n150906_ss.jpg</a></li>
...
Run Code Online (Sandbox Code Playgroud)
列表一直在继续.我的目标是只抓取结尾的列表项,_ss.jpg以便我可以渲染我的结果并在页面上很好地显示它们以进行演示.
我可以使用BeautifulSoup抓取页面,但从那里,我不知道如何过滤掉只匹配特定模式的列表项.该页面位于Basic Auth之后,我在之前关于BeautifulSoup的问题中已经解决了这个问题.我很高兴不使用它.
有任何想法吗?
需要使用re模块在Python中的href属性标记之间拉取字符串.
我尝试了很多模式,例如:
patFinderLink = re.compile('\>"(CVE.*)"\<\/a>')
Run Code Online (Sandbox Code Playgroud)
示例:我需要从以下标签中拉出标签之间的内容(在本例中为" CVE-2010-3718 "):
<pre>
<a href="https://www.redhat.com/security/data/cve/CVE-2010-3718.html">CVE-2010-3718</a>
</pre>
Run Code Online (Sandbox Code Playgroud)
我在这做错了什么?任何意见是极大的赞赏.先感谢您.
太阳
我正在尝试抓取此页面
我的汤选择器是:
test = soup.select('#bodyContent > #mw-content-text > table.wikitable:nth-of-type(4)')
Run Code Online (Sandbox Code Playgroud)
这应该返回#cmw-content-text的第四个子表。
但它返回一个空列表。
但是如果我查询:
test = soup.select('#bodyContent > #mw-content-text > table.wikitable')[3]
Run Code Online (Sandbox Code Playgroud)
我得到相同的选择器。
我在实施过程中缺少什么?
我正在使用以下代码<script>...</script>从网页获取所有内容(请参见代码中的url):
import urllib2
from bs4 import BeautifulSoup
import re
import imp
url = "http://racing4everyone.eu/2015/10/25/formula-e-201516formula-e-201516-round01-china-race/"
page = urllib2.urlopen(url)
soup = BeautifulSoup(page.read())
script = soup.find_all("script")
print script #just to check the output of script
Run Code Online (Sandbox Code Playgroud)
但是,BeautifulSoup会在网页的源代码(镶边中为Ctrl + U)内搜索。但是,我想在网页的元素代码(Chrome中为Ctrl + Shift + I)内进行BeautifulSoup搜索。
我希望它这样做是因为我真正感兴趣的代码是元素代码,而不是源代码。
我想使用Beautifulsoup修改整个divHTML。我试图修改HTML,但是控制台输出进行了修改,但是实际的.html文档本身并未被修改。没有创建新的HTML。
有人能帮我吗?
from bs4 import BeautifulSoup,Tag
import re
import urllib2
import os.path
base=os.path.dirname(os.path.abspath(__file__))
html=open(os.path.join(base,'example.html'))
soup=BeautifulSoup(html,'html.parser')
for i in soup.find('div',{"id":None}).findChildren():
l=str(i);
print l
print l.replace(l,'##')
Run Code Online (Sandbox Code Playgroud) 我试图摆脱这个for循环,而是使用list comprehension来给出相同的结果.
fd= nltk.FreqDist()
html = requests.get("http://www.nrc.nl/nieuws/2015/04/19/louise-gunning-vertrekt-als-voorzitter-bestuur-uva/")
raw = BeautifulSoup(html.text).text
for word in nltk.word_tokenize(raw):
freqdist[word.lower()] += 1
Run Code Online (Sandbox Code Playgroud)
我不确定它是否可能,但由于+ = 1,我无法让它工作.我试过了:
[freqdist[word.lower()] +=1 for word in nltk.word_tokenize(raw)]
Run Code Online (Sandbox Code Playgroud)
但这只会引发错误.有人能指出我正确的方向吗?
我试图理解分裂功能,我试图在网上找到,但我找不到关于分裂的实用例子的好文档,所以我在这里问了几个问题,这样它会帮助初学者试图学习分裂:
首先,我混淆了splinter中的实际css选择器,我看到了两种方法:
browser.find_by_css()
Run Code Online (Sandbox Code Playgroud)
要么
browser.find_by_css_selector()
Run Code Online (Sandbox Code Playgroud)
它们之间有什么区别,为什么第二个不能在当前的分裂中工作?
现在我的原始问题是如何选择任何类下的任何标签,如何选择任何ID下的任何标签?
我试图找到,但我发现大多数stackoverflow问题的分裂"如何在下拉列表中选择选项值",并且分裂文档非常好,但问题是他们的方法没有足够实用的示例.
所以,如果我有这个HTML代码:
<div class="medium-widget success-story-category">
<h2 class="widget-title"><span aria-hidden="true" class="icon-get-started"></span>Getting Started</h2>
<p>Python can be easy to pick up whether you're a first time programmer or you're experienced with other languages. The following pages are a useful first step to get on your way writing programs with Python!</p>
<ul>
<li><a href="https://wiki.python.org/moin/BeginnersGuide/Programmers">Beginner's Guide, Programmers</a></li>
<li><a href="https://wiki.python.org/moin/BeginnersGuide/NonProgrammers">Beginner's Guide, Non-Programmers</a></li>
<li><a href="https://wiki.python.org/moin/BeginnersGuide/Download">Beginner's Guide, Download & Installation</a></li>
<li><a href="https://wiki.python.org/moin/BeginnersGuide/Examples">Code sample and snippets for Beginners</a></li>
</ul>
</div>
Run Code Online (Sandbox Code Playgroud)
然后 :
如何通过选择选择 …
我已经捕获了以下html使用BS4,但似乎无法搜索艺术家标签.我已经将这个代码块分配给一个名为container的变量,然后尝试了
print container.tr.td["artist"]
Run Code Online (Sandbox Code Playgroud)
没有运气.有什么建议表赞赏
<tr class="item">
<!-- <td class="image"><a href="https://www.stargreen.com/kool-as-the-gang-44415.html" title="KOOL AS THE GANG " class="product-image"><img src="https://www.stargreen.com/media/catalog/product/cache/1/small_image/135x/9df78eab33525d08d6e5fb8d27136e95/K/o/KoolAsTheGang.jpg" width="135" height="135" alt="KOOL AS THE GANG " /></a></td> -->
<td class="date">Sat, 30 Dec 2017</td>
<td class="artist">kool as the gang</td>
<td class="venue">100 club</td>
<td class="link">
<p class="availability out-of-stock">
<span>Off Sale</span></p>
</td>
</tr>
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Beautiful汤来定位具有非标准属性的DIV。这是DIV:
`<div data-asin="099655596X" data-index="1" class="sg-col-20-of-24 s-result-item sg-col-0-of-12 sg-col-28-of-32 sg-col-16-of-20 sg-col sg-col-32-of-36 sg-col-12-of-16 sg-col-24-of-28" data-cel widget="search_result_1">`
Run Code Online (Sandbox Code Playgroud)
我需要使用data-asin属性找到find_all DIV,并同时获取asin。BS似乎支持此功能,但是我正在做的事没有用。这是我的代码不起作用:
`rows = soup.find_all(attrs={"data-asin": "value"})`
Run Code Online (Sandbox Code Playgroud)
我如何在Python3.7中制作BS来查找所有这些DIV?
beautifulsoup ×10
python ×10
html ×4
regex ×2
web-scraping ×2
automation ×1
javascript ×1
nltk ×1
selenium ×1
splinter ×1