Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:在关键字之间解析文本_Python_Regex_Web Scraping_Beautifulsoup - Fatal编程技术网

Python:在关键字之间解析文本

Python:在关键字之间解析文本,python,regex,web-scraping,beautifulsoup,Python,Regex,Web Scraping,Beautifulsoup,我正在寻求使用BeautifulSoup解析一类网页上的文本,代码如下: import urllib import re html = urllib.urlopen('http://english.hani.co.kr/arti/english_edition/e_national/714507.html').read() content= str(soup.find("div", class_="article-contents")) 所以我的目标是至少解析出第一段中的第一句或前几个句

我正在寻求使用BeautifulSoup解析一类网页上的文本,代码如下:

import urllib 
import re

html = urllib.urlopen('http://english.hani.co.kr/arti/english_edition/e_national/714507.html').read()
content= str(soup.find("div",  class_="article-contents"))
所以我的目标是至少解析出第一段中的第一句或前几个句子

因为段落没有被
标记包围,所以到目前为止,我最好的策略是在内容中找到介于
之间的文本(恰好是第一段)

以下是目标文本的样子:

<div class="article-contents">
<div class="article-alignC">
<table class="photo-view-area">
<tr>
<td>
<img alt="" border="0" src="http://img.hani.co.kr/imgdb/resize/2015/1024/00542577201_20151024.JPG" style="width:590px;"/>
</td>
</tr>
</table>
</div>
<h4></h4>

(这是我要解析的内容,介于
之间)


我正试图直接在BeautifulSoup或使用正则表达式上执行此操作,但到目前为止仍然没有成功。

找到
h4
元素,并使用以下方法查找下一个第一个文本同级:

印刷品:

US scholar argues that any government attempt to impose single view of history is misguided On Oct. 19, the Hankyoreh’s Washington correspondent conducted on interview with phone and email with William North, chair of the history department at Carleton University in Minnesota. The main topic of the discussion was the efforts of the administration of South Korean President Park Geun-hye to take over the production of history textbooks. 

事实上,在这里,仅仅使用就足够了:

print(h4.next_sibling)

非常感谢,虽然这里没有必要,但知道(text=true)也很好!还有一个问题:在相同的设置下,是否有方法返回第二段而不是第一段?@carl\u pch还没有测试,但请尝试一下:
h4.查找下一段兄弟姐妹(text=True)[1]
。这太棒了!我读到了反复出现的“next#u兄弟姐妹”,但没有意识到[#]可以表示多少次(如果我理解正确的话)。再次感谢。
print(h4.next_sibling)