Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.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中从html标记中提取字符串_Python_Regex_Beautifulsoup - Fatal编程技术网

在python中从html标记中提取字符串

在python中从html标记中提取字符串,python,regex,beautifulsoup,Python,Regex,Beautifulsoup,希望没有一个重复的问题,我已经看过了,因为我一直在搜索这个论坛的人谁已经张贴到一个类似的下面一个 基本上,我已经创建了一个python脚本,它将从下面显示的url中刮取每艘船的呼号,并将它们附加到一个列表中。简而言之,它是有效的,但是每当我遍历列表并显示每个元素时,每个呼号之间似乎都有一个“[”和“]”。我已在下面显示了脚本的输出: 输出 *********************** Contents of 'listOfCallSigns' List ************

希望没有一个重复的问题,我已经看过了,因为我一直在搜索这个论坛的人谁已经张贴到一个类似的下面一个

基本上,我已经创建了一个python脚本,它将从下面显示的url中刮取每艘船的呼号,并将它们附加到一个列表中。简而言之,它是有效的,但是每当我遍历列表并显示每个元素时,每个呼号之间似乎都有一个“[”和“]”。我已在下面显示了脚本的输出:

输出

***********************     Contents of 'listOfCallSigns' List     ***********************

0 ['311062900']
1 ['235056239']
2 ['305500000']
3 ['311063300']
4 ['236111791']
5 ['245639000']
6 ['235077805']
7 ['235011590']
# Importing the modules needed to run the script 
from bs4 import BeautifulSoup
import urllib2
import re
import requests
import pprint


# Declaring the url for the port of hull
url = "http://www.fleetmon.com/en/ports/Port_of_Hull_5898"


# Opening and reading the contents of the URL using the module 'urlib2'
# Scanning the entire webpage, finding a <table> tag with the id 'vessels_in_port_table' and finding all <tr> tags
portOfHull = urllib2.urlopen(url).read()
soup = BeautifulSoup(portOfHull)
table = soup.find("table", {'id': 'vessels_in_port_table'}).find_all("tr")


# Declaring a list to hold the call signs of each ship in the table
listOfCallSigns = []


# For each row in the table, using a regular expression to extract the first 9 numbers from each ship call-sign
# Adding each extracted call-sign to the 'listOfCallSigns' list
for i, row in enumerate(table):
    if i:
        listOfCallSigns.append(re.findall(r"\d{9}", str(row.find_all('td')[4])))


print "\n\n***********************     Contents of 'listOfCallSigns' List     ***********************\n"

# Printing each element of the 'listOfCallSigns' list
for i, row in enumerate(listOfCallSigns):
    print i, row  
如您所见,它显示了每个呼号的方括号。我有一种感觉,这可能是由于BeautifulSoup库中的编码问题

理想情况下,我希望输出没有任何方括号,只将呼号作为字符串。

***********************     Contents of 'listOfCallSigns' List     ***********************

0 311062900
1 235056239
2 305500000
3 311063300
4 236111791
5 245639000
6 235077805
7 235011590
我当前使用的脚本如下所示:

我的脚本

***********************     Contents of 'listOfCallSigns' List     ***********************

0 ['311062900']
1 ['235056239']
2 ['305500000']
3 ['311063300']
4 ['236111791']
5 ['245639000']
6 ['235077805']
7 ['235011590']
# Importing the modules needed to run the script 
from bs4 import BeautifulSoup
import urllib2
import re
import requests
import pprint


# Declaring the url for the port of hull
url = "http://www.fleetmon.com/en/ports/Port_of_Hull_5898"


# Opening and reading the contents of the URL using the module 'urlib2'
# Scanning the entire webpage, finding a <table> tag with the id 'vessels_in_port_table' and finding all <tr> tags
portOfHull = urllib2.urlopen(url).read()
soup = BeautifulSoup(portOfHull)
table = soup.find("table", {'id': 'vessels_in_port_table'}).find_all("tr")


# Declaring a list to hold the call signs of each ship in the table
listOfCallSigns = []


# For each row in the table, using a regular expression to extract the first 9 numbers from each ship call-sign
# Adding each extracted call-sign to the 'listOfCallSigns' list
for i, row in enumerate(table):
    if i:
        listOfCallSigns.append(re.findall(r"\d{9}", str(row.find_all('td')[4])))


print "\n\n***********************     Contents of 'listOfCallSigns' List     ***********************\n"

# Printing each element of the 'listOfCallSigns' list
for i, row in enumerate(listOfCallSigns):
    print i, row  
#导入运行脚本所需的模块
从bs4导入BeautifulSoup
导入urllib2
进口稀土
导入请求
导入pprint
#声明hull端口的url
url=”http://www.fleetmon.com/en/ports/Port_of_Hull_5898"
#使用模块“urlib2”打开并读取URL的内容
#扫描整个网页,查找id为“Vessers_in_port_table”的标签并查找所有标签
portOfHull=urllib2.urlopen(url.read)()
汤=美汤(波尔图港)
table=soup.find(“table”,{'id':'vessers\u in_port\u table')。find_all(“tr”)
#声明一个列表来保存表中每艘船的呼号
listOfCallSigns=[]
#对于表中的每一行,使用正则表达式从每个ship呼号中提取前9个数字
#将每个提取的呼号添加到“listOfCallSigns”列表
对于i,枚举(表)中的行:
如果我:
append(re.findall(r“\d{9}”),str(row.find_all('td')[4]))
打印“\n\n******************************”列表的内容*********************************\n”
#打印“listOfCallSigns”列表的每个元素
对于i,枚举中的行(调用符号列表):
打印i,第行
有人知道如何删除每个呼号周围的方括号并只显示字符串吗?


提前感谢!:)

将最后几行更改为:

# Printing each element of the 'listOfCallSigns' list
for i, row in enumerate(listOfCallSigns):
    print i, row[0]  # <-- added a [0] here
这里的解释是,
re.findall(…)
返回一个列表(在您的示例中,其中包含一个元素)。因此,
listOfCallSigns
最终成为“每个子列表包含一个字符串的列表”:

当您
枚举
您的
调用符号列表
时,
变量基本上就是您前面在代码中附加的
re.findall(…)
(这就是为什么您可以在它们之后添加
[0]

因此,
row
re.findall(…)
都属于“字符串列表”类型,如下所示:

>>> row
>>> ['311062900']
要获取列表中的字符串,需要访问其第一个元素,即:

>>> row[0]
>>> '311062900'

希望这有帮助

将最后几行更改为:

# Printing each element of the 'listOfCallSigns' list
for i, row in enumerate(listOfCallSigns):
    print i, row[0]  # <-- added a [0] here
这里的解释是,
re.findall(…)
返回一个列表(在您的示例中,其中包含一个元素)。因此,
listOfCallSigns
最终成为“每个子列表包含一个字符串的列表”:

当您
枚举
您的
调用符号列表
时,
变量基本上就是您前面在代码中附加的
re.findall(…)
(这就是为什么您可以在它们之后添加
[0]

因此,
row
re.findall(…)
都属于“字符串列表”类型,如下所示:

>>> row
>>> ['311062900']
要获取列表中的字符串,需要访问其第一个元素,即:

>>> row[0]
>>> '311062900'

希望这有帮助

将最后几行更改为:

# Printing each element of the 'listOfCallSigns' list
for i, row in enumerate(listOfCallSigns):
    print i, row[0]  # <-- added a [0] here
这里的解释是,
re.findall(…)
返回一个列表(在您的示例中,其中包含一个元素)。因此,
listOfCallSigns
最终成为“每个子列表包含一个字符串的列表”:

当您
枚举
您的
调用符号列表
时,
变量基本上就是您前面在代码中附加的
re.findall(…)
(这就是为什么您可以在它们之后添加
[0]

因此,
row
re.findall(…)
都属于“字符串列表”类型,如下所示:

>>> row
>>> ['311062900']
要获取列表中的字符串,需要访问其第一个元素,即:

>>> row[0]
>>> '311062900'

希望这有帮助

将最后几行更改为:

# Printing each element of the 'listOfCallSigns' list
for i, row in enumerate(listOfCallSigns):
    print i, row[0]  # <-- added a [0] here
这里的解释是,
re.findall(…)
返回一个列表(在您的示例中,其中包含一个元素)。因此,
listOfCallSigns
最终成为“每个子列表包含一个字符串的列表”:

当您
枚举
您的
调用符号列表
时,
变量基本上就是您前面在代码中附加的
re.findall(…)
(这就是为什么您可以在它们之后添加
[0]

因此,
row
re.findall(…)
都属于“字符串列表”类型,如下所示:

>>> row
>>> ['311062900']
要获取列表中的字符串,需要访问其第一个元素,即:

>>> row[0]
>>> '311062900'

希望这有帮助

这也可以通过从字符串中剥离不需要的字符来实现,如下所示:

a = "string with bad characters []'] in here" 
a = a.translate(None, "[]'")
print a 

这也可以通过从字符串中删除不需要的字符来实现,如下所示:

a = "string with bad characters []'] in here" 
a = a.translate(None, "[]'")
print a 

这也可以通过从字符串中删除不需要的字符来实现,如下所示:

a = "string with bad characters []'] in here" 
a = a.translate(None, "[]'")
print a 

这也可以通过从字符串中删除不需要的字符来实现,如下所示:

a = "string with bad characters []'] in here" 
a = a.translate(None, "[]'")
print a 

明亮的这成功了!我很好奇,你能解释一下为什么插入第[0]行会删除括号吗?现在它是否将每个列表元素作为字符串返回?我编辑了答案,添加了一些关于如何从列表中获取字符串的详细信息。太棒了!非常感谢你!明亮的这成功了!我很好奇,你能解释一下为什么要插入