Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/357.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_Html_Web Scraping - Fatal编程技术网

Python 如何提取两个角色之间的兴趣值?

Python 如何提取两个角色之间的兴趣值?,python,html,web-scraping,Python,Html,Web Scraping,我正在使用以下HTML代码进行网页抓取: Predecessors &middot; <i class="fa fa-sign-in"></i> / Successors &middot; <i class="fa fa-sign-out"></i> </dt> <dd> 1931 &middot; <a class="active" href="../../../aus/pa

我正在使用以下HTML代码进行网页抓取:

Predecessors &middot; <i class="fa fa-sign-in"></i> / Successors &middot; <i class="fa fa-sign-out"></i>
</dt>

<dd>
    1931 &middot;
    <a class="active" href="../../../aus/party/1253">
                ALP </a> &middot;
    <i class="fa fa-sign-in"> </i> splinter

</dd>

<dd>
    1931 &middot;
    <a class="active" href="../../../aus/party/1905">
                NAT </a> &middot;
    <i class="fa fa-sign-in"> </i> successor

</dd>
我用于获取上述输出的代码如下所示:

import urllib.request

url_pc = str('http://www.parlgov.org/explore/aus/party/1912/")
fp = urllib.request.urlopen(url_pc)
mybytes = fp.read()

mystr = mybytes.decode("utf8")
fp.close()
#print(mystr)

str1 = mystr[mystr.find('Predecessors'):]

str2 = str1.split("</div>", 1)[0]

str3 = str2.split("<dt> Party (name) changes</dt>", 1)[0]

print(str3)
我想提取每个组中介于和之间的所有内容,将其转换为字符串,然后将其添加到一行数据中。是否可以运行循环或使用代码来提取两个组之间和中的所有字符串?

您可以使用BeautifuSoup查找所有字符串,然后获取每个as列表的内容。然后可以将列表中的元素连接到一个字符串。某些元素可以是需要转换为字符串的对象。您也可以使用条带删除一些空间,但可能仍需要进行清理

text = '''Predecessors &middot; <i class="fa fa-sign-in"></i>
            / Successors &middot; <i class="fa fa-sign-out"></i>
          </dt>

            <dd>
              1931 &middot;
              <a class="active"
                 href="../../../aus/party/1253">
                ALP </a>

              &middot;
               <i class="fa fa-sign-in"> </i> 

               splinter 



            </dd>

            <dd>
              1931 &middot;
              <a class="active"
                 href="../../../aus/party/1905">
                NAT </a>

              &middot;
               <i class="fa fa-sign-in"> </i> 

               successor 



            </dd>'''

from bs4 import BeautifulSoup as BS

soup = BS(text, 'html.parser')

for item in soup.find_all('dd'):
    print(''.join(str(x).strip() for x in item.contents))
结果:

1931, ALP, splinter, 1253
1931, NAT, successor, 1905
1931, ALP, splinter, 1253 | 1931, NAT, successor, 1905

显示您的代码。你使用什么模块-lxml、BeautifulSoup、Selenium、Scrapy、other?你想要所有HTML还是只想要没有HTML标签的文本?添加了我使用的代码。理想情况下,我想要1931·&米德多;斯普林特与1931·&米德多;完美!最终目标是将其转换为以下格式:1931年,阿尔卑斯山,斯普林特,1253年| 1931年,纳特,继任者,1905年。这是朝着这个方向迈出的一大步!再次感谢。item.contents给出了带有字符串1931和splinter的列表。item.find'a.get'href'提供字符串../../../aus/party/1253,您必须对其进行切片[-4:]或拆分'/'。item.find'a'。get_text给出ALP-所以您有所有元素来创建字符串。请参阅答案中的新代码。它给出了弦1931,阿尔普,斯普林特,1253 | 1931,纳特,继任者,1905
from bs4 import BeautifulSoup as BS

soup = BS(text, 'html.parser')

all_rows = []

for item in soup.find_all('dd'):
    #print(''.join(str(x).strip() for x in item.contents))
    row = (item.contents[0].strip()[:-2], item.find('a').get_text().strip(), item.contents[4].strip(), item.find('a').get('href')[-4:])
    row = ', '.join(row)
    print(row)
    all_rows.append(row)

text = ' | '.join(all_rows)
print(text)
1931, ALP, splinter, 1253
1931, NAT, successor, 1905
1931, ALP, splinter, 1253 | 1931, NAT, successor, 1905