我正在尝试编写一些 BeautifulSoup 代码,它将采用标签包围的每一段文本并将标签更改为标签 - 但是,只有当它只是一行没有其他书面/输出文本时。
这可能吗?

对此

但这将保持不变:

我知道以下内容将有助于改变所有强者。我怎样才能只获取重要的?
import BeautifulSoup
if __name__ == "__main__":
data = """
<html>
<h2 class='someclass'>some title</h2>
<ul>
<li>Lorem ipsum dolor sit amet, consectetuer adipiscing elit.</li>
<li>Aliquam tincidunt mauris eu risus.</li>
<li>Vestibulum auctor dapibus neque.</li>
</ul>
</html>
"""
soup = BeautifulSoup.BeautifulSoup(data)
h2 = soup.find('strong')
h2.name = 'h1'
print soup
Run Code Online (Sandbox Code Playgroud) 我试图根据某种模式拆分一些 HTML。
HTML 的特定部分必须分为 1 个或多个部分或文本数组。我能够划分此 HTML 的方法是查看第一个<strong>和一个 double <br />。这两个标签之间的所有文本都必须放入列表中并进行迭代。
如何轻松解决这个问题?
所以我想要以下 HTML:
<div class="clearfix">
<!--# of ppl associated with place-->
This is some kind of buzzword:<br />
<br />
<!--Persontype-->
<strong>Jimbo</strong> Jack <br />
Some filler text <br />
More weird stuff
<br />
Unstructured text <br />
<br />
<strong>Jacky</strong> Bradson <br />
This is just a test <br />
Nothing but a test
<br />
More unstructured stuff <br />
<br />
<strong>Junior</strong> …Run Code Online (Sandbox Code Playgroud) 我的字符串 html 中有一些段落如下所示:
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
</p>
Run Code Online (Sandbox Code Playgroud)
我想删除标签内的空白p并将其变成:
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua</p>
Run Code Online (Sandbox Code Playgroud)
请注意,p像这样的标签应该保持更改:
<p class="has-media media-640"><img alt="Lorem ipsum dolor sit amet" height="357" src="http://www.example.com/img/lorem.jpg" width="636"/></p>
Run Code Online (Sandbox Code Playgroud)
我想要的是:
for p in soup.findAll('p'):
replace p.string with trimmed text
Run Code Online (Sandbox Code Playgroud) 尝试编写一些代码,首先将玩家的姓名与他的工资请求相匹配。我能够编写它,以便通过从“sortcell”类中调用它来获取给定团队中每个球员的名字,但我似乎不知道如何获得薪水,因为他们都被称为 .
from bs4 import BeautifulSoup
from urllib import urlopen
teams = ['http://espn.go.com/nba/team/roster/_/name/atl/atlanta-hawks']
for team in teams:
html = urlopen('' + team)
soup = BeautifulSoup(html.read(), 'lxml')
names = soup.findAll("td", {"class": "sortcell"})
salary = soup.findAll("td", {"class": "td"})
print(salary)
for i in range(1, 15):
name = names[i].get_text()
print(name)
Run Code Online (Sandbox Code Playgroud)
您可以在以“薪水”开头的代码中看到我的(失败的)尝试。关于如何只获得薪资级别有什么想法吗?谢谢!
预期行为:
Salary 变量应该返回给定球员的工资,但目前不返回任何内容。
各位晚上好,
我试图向 Google 提出一个问题,并从其受尊重的搜索查询中提取所有相关链接(即我搜索“site: Wikipedia.com Thomas Jefferson”,它给了我 wiki.com/jeff、wiki.com/tom、 ETC。)
这是我的代码:
from bs4 import BeautifulSoup
from urllib2 import urlopen
query = 'Thomas Jefferson'
query.replace (" ", "+")
#replaces whitespace with a plus sign for Google compatibility purposes
soup = BeautifulSoup(urlopen("https://www.google.com/?gws_rd=ssl#q=site:wikipedia.com+" + query), "html.parser")
#creates soup and opens URL for Google. Begins search with site:wikipedia.com so only wikipedia
#links show up. Uses html parser.
for item in soup.find_all('h3', attrs={'class' : 'r'}):
print item.string
#Guides BS to h3 class "r" where green Wikipedia …Run Code Online (Sandbox Code Playgroud) 我正在尝试抓取 OldNavy 网页上产品的网址。然而,它只给出了产品列表的一部分,而不是整个列表(例如,当 URL 远远超过 8 个时,只给出 8 个)。我希望有人可以帮助并找出问题所在。
from bs4 import BeautifulSoup
from selenium import webdriver
import html5lib
import platform
import urllib
import urllib2
import json
link = http://oldnavy.gap.com/browse/category.do?cid=1035712&sop=true
base_url = "http://www.oldnavy.com"
driver = webdriver.PhantomJS()
driver.get(link)
html = driver.page_source
soup = BeautifulSoup(html, "html5lib")
bigDiv = soup.findAll("div", class_="sp_sm spacing_small")
for div in bigDiv:
links = div.findAll("a")
for i in links:
j = j + 1
productUrl = base_url + i["href"]
print productUrl
Run Code Online (Sandbox Code Playgroud) python beautifulsoup web-crawler web-scraping selenium-webdriver
我需要你的帮助:我有<p>带有许多其他标签的标签,如下例所示:
<p>I <strong>AM</strong> a <i>text</i>.</p>
Run Code Online (Sandbox Code Playgroud)
我只想得到“我是文本”,所以我 unwrap() 标签strong并i
使用下面的代码:
for elem in soup.find_all(['strong', 'i']):
elem.unwrap()
Run Code Online (Sandbox Code Playgroud)
接下来,如果我打印一切soup.p都正确,但如果我不知道我的字符串所在的标签的名称,问题就会开始!
下面的代码应该更清楚:
from bs4 import BeautifulSoup
html = '''
<html>
<header></header>
<body>
<p>I <strong>AM</strong> a <i>text</i>.</p>
</body>
</html>
'''
soup = BeautifulSoup(html, 'lxml')
for elem in soup.find_all(['strong', 'i']):
elem.unwrap()
print soup.p
# output :
# <p>I AM a text.</p>
for s in soup.stripped_strings:
print s
# output
'''
I
AM
a
text
.
'''
Run Code Online (Sandbox Code Playgroud)
为什么 BeautifulSoup 将我的所有字符串分开,而我之前将它与我的 unwrap() …
您可以在该维基百科页面左侧边栏的“工具”下找到即维基数据项。如果将鼠标悬停在该 上,您可以找到如下链接地址,末尾带有 Q 号。 https://www.wikidata.org/wiki/Special:EntityPage/Q15112。如何提取 Q 号码?
from bs4 import BeautifulSoup
import requests
getUrl= 'https://en.wikipedia.org/wiki/Ariyalur_district'
url = getUrl
content = requests.get(url).content
soup = BeautifulSoup(content,'lxml')
#extracting page title
firstHeading = soup.find('h1',{'class' : 'firstHeading'})
print(firstHeading.text +'~')
Run Code Online (Sandbox Code Playgroud)
到目前为止,我的代码很好。我试图通过下面的代码获取 Q 号码,但我不能。请指导我。
QNumber = soup.find('li','t-wikibase')
print(QNumber)
Run Code Online (Sandbox Code Playgroud)
如何获得Q号码?
我正在尝试使用 python 解析网页并创建站点地图。我写了下面的代码 -
import urllib2
from bs4 import BeautifulSoup
mypage = "http://example.com/"
page = urllib2.urlopen(mypage)
soup = BeautifulSoup(page,'html.parser')
all_links = soup.find_all('a')
for link in all_links:
print link.get('href')
Run Code Online (Sandbox Code Playgroud)
上面的代码打印了(外部和内部)中的所有链接example.com。
"example.com"有链接到"example.com/page1",有链接到"example.com/page3"。为这种流程创建地图的理想方法是什么?我正在寻找一个显示"example.com" -> "example.com/page1" -> "example.com/page3"或类似内容的库或逻辑我检查了类似的问题,但找不到解决方案......
我正在尝试从以下页面中获取额外旅行时间的分钟数 (46):https://www.tomtom.com/en_gb/trafficindex/city/istanbul
我尝试了两种方法(Xpath 和 find 类),但都给出了空返回。
import requests
from bs4 import BeautifulSoup
from lxml.html import fromstring
page = requests.get("https://www.tomtom.com/en_gb/trafficindex/city/istanbul")
tree = fromstring(page.content)
soup = BeautifulSoup(page.content, 'html.parser')
#print([type(item) for item in list(soup.children)])
html = list(soup.children)[2]
g_data = soup.find_all("div", {"class_": "big.ng-binding"})
congestion = tree.xpath("/html/body/div/div[2]/div[2]/div[2]/section[2]/div/div[2]/div/div[2]/div/div[2]/div[1]/div[1]/text()")
print(congestion)
print(len(g_data))
Run Code Online (Sandbox Code Playgroud)
我错过了一些明显的东西吗?
非常感谢您的帮助!
beautifulsoup ×10
python ×8
html ×4
python-3.x ×2
web-crawler ×2
class ×1
html-parsing ×1
javascript ×1
string ×1
trim ×1
web-scraping ×1
wikipedia ×1