假设我有一个页面div.我可以很容易地得到那个div soup.find().
现在我已经得到了结果,我想要打印出innerhtml它的全部内容div:我的意思是,我需要一个包含所有html标签和文本的字符串,就像我在javascript中获得的字符串一样obj.innerHTML.这可能吗?
编程新手并想出如何使用Selenium导航到我需要去的地方.我想现在解析数据,但不知道从哪里开始.有人能握住我的手一秒钟并指出我正确的方向吗?
任何帮助表示赞赏
我的网页是这样的 -
<p>
<strong class="offender">YOB:</strong> 1987<br/>
<strong class="offender">RACE:</strong> WHITE<br/>
<strong class="offender">GENDER:</strong> FEMALE<br/>
<strong class="offender">HEIGHT:</strong> 5'05''<br/>
<strong class="offender">WEIGHT:</strong> 118<br/>
<strong class="offender">EYE COLOR:</strong> GREEN<br/>
<strong class="offender">HAIR COLOR:</strong> BROWN<br/>
</p>
Run Code Online (Sandbox Code Playgroud)
我要提取的信息对每个人,并得到了YOB:1987,RACE:WHITE等....
我试过的是 -
subc = soup.find_all('p')
subc1 = subc[1]
subc2 = subc1.find_all('strong')
Run Code Online (Sandbox Code Playgroud)
但是,这给我的唯一的值YOB:,RACE:等
有没有一种方法,我可以得到的数据YOB:1987,RACE:WHITE格式?
我正在尝试<p>使用BeautifulSoup 从网页中的元素中删除所有内部html .有内部标签,但我不在乎,我只想获得内部文本.
例如,对于:
<p>Red</p>
<p><i>Blue</i></p>
<p>Yellow</p>
<p>Light <b>green</b></p>
Run Code Online (Sandbox Code Playgroud)
我怎样才能提取:
Red
Blue
Yellow
Light green
Run Code Online (Sandbox Code Playgroud)
我既不需.string也不.contents[0]需要.也不是.extract(),因为我不想提前指定内部标签 - 我想处理任何可能发生的事情.
BeautifulSoup中是否有'just get the visible HTML'类型的方法?
---- ------ UPDATE
在建议上,尝试:
soup = BeautifulSoup(open("test.html"))
p_tags = soup.findAll('p',text=True)
for i, p_tag in enumerate(p_tags):
print str(i) + p_tag
Run Code Online (Sandbox Code Playgroud)
但这没有帮助 - 它打印出来:
0Red
1
2Blue
3
4Yellow
5
6Light
7green
8
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用BeautifulSoup转换一大块HTML文本.这是一个例子:
<div>
<p>
Some text
<span>more text</span>
even more text
</p>
<ul>
<li>list item</li>
<li>yet another list item</li>
</ul>
</div>
<p>Some other text</p>
<ul>
<li>list item</li>
<li>yet another list item</li>
</ul>
Run Code Online (Sandbox Code Playgroud)
我尝试过这样的事情:
def parse_text(contents_string)
Newlines = re.compile(r'[\r\n]\s+')
bs = BeautifulSoup.BeautifulSoup(contents_string, convertEntities=BeautifulSoup.BeautifulSoup.HTML_ENTITIES)
txt = bs.getText('\n')
return Newlines.sub('\n', txt)
Run Code Online (Sandbox Code Playgroud)
...但是那样我的span元素总是在新的一行上.这当然是一个简单的例子.有没有办法让HTML页面中的文本在浏览器中呈现的方式(不需要css规则,只有常规的div,span,li等元素呈现)?
我正在练习'Web Scraping with Python'的代码,我一直有这个证书问题:
from urllib.request import urlopen
from bs4 import BeautifulSoup
import re
pages = set()
def getLinks(pageUrl):
global pages
html = urlopen("http://en.wikipedia.org"+pageUrl)
bsObj = BeautifulSoup(html)
for link in bsObj.findAll("a", href=re.compile("^(/wiki/)")):
if 'href' in link.attrs:
if link.attrs['href'] not in pages:
#We have encountered a new page
newPage = link.attrs['href']
print(newPage)
pages.add(newPage)
getLinks(newPage)
getLinks("")
Run Code Online (Sandbox Code Playgroud)
错误是:
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 1319, in do_open
raise URLError(err)
urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1049)>
Run Code Online (Sandbox Code Playgroud)
顺便说一句,我也在练习scrapy,但一直都在解决问题:找不到命令:scrapy(我在网上尝试过各种解决方案,但都没有用......真的很令人沮丧)
我正在尝试使用以下内容获取内容"我的家庭地址",但得到了AttributeError:
address = soup.find(text="Address:")
print address.nextSibling
Run Code Online (Sandbox Code Playgroud)
这是我的HTML:
<td><b>Address:</b></td>
<td>My home address</td>
Run Code Online (Sandbox Code Playgroud)
导航td标记和拉取内容的好方法是什么?
我有使用Python 2.7的windows vista.我想安装BeautifulSoup 4,但显然我只是将文件复制到site-packages目录中就无法安装Beautiful Soup.我必须安装pip然后从命令提示符运行一些命令.你能一步一步指导我吗?我真的是一个菜鸟所以让它变得非常简单.提前致谢
我正在寻找一种方法来使用findAll按照它们在页面上显示的顺序获取两个标签.
目前我有:
import requests
import BeautifulSoup
def get_soup(url):
request = requests.get(url)
page = request.text
soup = BeautifulSoup(page)
get_tags = soup.findAll('hr' and 'strong')
for each in get_tags:
print each
Run Code Online (Sandbox Code Playgroud)
如果我在一个只有'em'或'strong'的页面上使用它,那么它将为我提供所有这些标签,如果我在两者上使用它将获得'强'标签.
有没有办法做到这一点?我主要关注的是保留标签的查找顺序.
首先,对于Python来说,我是一个完全新手.但是,我编写了一段代码来查看RSS提要,打开链接并从文章中提取文本.这是我到目前为止:
from BeautifulSoup import BeautifulSoup
import feedparser
import urllib
# Dictionaries
links = {}
titles = {}
# Variables
n = 0
rss_url = "feed://www.gfsc.gg/_layouts/GFSC/GFSCRSSFeed.aspx?Division=ALL&Article=All&Title=News&Type=doc&List=%7b66fa9b18-776a-4e91-9f80- 30195001386c%7d%23%7b679e913e-6301-4bc4-9fd9-a788b926f565%7d%23%7b0e65f37f-1129-4c78-8f59-3db5f96409fd%7d%23%7bdd7c290d-5f17-43b7-b6fd-50089368e090%7d%23%7b4790a972-c55f-46a5-8020-396780eb8506%7d%23%7b6b67c085-7c25-458d-8a98-373e0ac71c52%7d%23%7be3b71b9c-30ce-47c0-8bfb-f3224e98b756%7d%23%7b25853d98-37d7-4ba2-83f9-78685f2070df%7d%23%7b14c41f90-c462-44cf-a773-878521aa007c%7d%23%7b7ceaf3bf-d501-4f60-a3e4-2af84d0e1528%7d%23%7baf17e955-96b7-49e9-ad8a-7ee0ac097f37%7d%23%7b3faca1d0-be40-445c-a577-c742c2d367a8%7d%23%7b6296a8d6-7cab-4609-b7f7-b6b7c3a264d6%7d%23%7b43e2b52d-e4f1-4628-84ad-0042d644deaf%7d"
# Parse the RSS feed
feed = feedparser.parse(rss_url)
# view the entire feed, one entry at a time
for post in feed.entries:
# Create variables from posts
link = post.link
title = post.title
# Add the link to the dictionary
n += 1
links[n] = link
for k,v in links.items():
# Open RSS feed …Run Code Online (Sandbox Code Playgroud) beautifulsoup ×10
python ×9
html ×2
web-scraping ×2
innerhtml ×1
parsing ×1
python-2.7 ×1
scrapy ×1
selenium ×1