Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/90.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 从BeautifulSoup对象中删除标记_Python_Html_Beautifulsoup - Fatal编程技术网

Python 从BeautifulSoup对象中删除标记

Python 从BeautifulSoup对象中删除标记,python,html,beautifulsoup,Python,Html,Beautifulsoup,我在从论坛网站(每页50行)上抓取表格时遇到了一些困难。正如我所写的,我的代码可以处理95%的内容,但当出现粗体或斜体行时,会添加一个表格分隔符,而我没有得到完整的结果 大多数页面的标签如下所示: <table> <td> content_1 </td><td> content_2 </td><td> content_3 </td> ... </table>

我在从论坛网站(每页50行)上抓取表格时遇到了一些困难。正如我所写的,我的代码可以处理95%的内容,但当出现粗体或斜体行时,会添加一个表格分隔符,而我没有得到完整的结果

大多数页面的标签如下所示:

<table>
  <td>
    content_1
  </td><td>
    content_2
  </td><td>
    content_3
  </td>
  ...
</table>

正如您已经注意到的,
是无序的,因此HTML格式不正确
html.parser
无法处理该作业。我已经尝试了针对您的测试用例的
lxml
,它是有效的

soup = BeautifulSoup(content, "lxml")

“…但出现粗体或斜体行时会中断…”请显示中断的代码和错误消息(如果有)。抱歉,“中断”是错误的词语选择。它只是没有提供其他页面提供的完整结果。我添加了我的代码以及示例页面…webite很混乱。。。你能澄清一下你想得到的网站的哪一部分,可能有一个更好的标签吗?是的,它非常混乱。。。只是想把表格放在页面底部,上面有帖子号、主题、用户名、日期……看起来就是这样。非常感谢。我习惯于总是默认使用html.parser。
def get_page(URL):
    '''
    INPUT: url
    OUTPUT: pandas dataframe with message board info
    '''
    content = requests.get(URL).content
    soup = BeautifulSoup(content, "html.parser")
    rows = list(soup.find('table', id="ctl00_CP1_gv"))
    table_lst = []
    for row in rows[2:-2]:
      cell_lst = [cell for cell in list(row)[1:5]]
      table_lst.append(cell_lst)
    return pd.DataFrame(table_lst)

if __name__ == '__main__':
    url1 = "https://investorshub.advfn.com/Cal-Bay-International-Inc-CBYI-5520/?NextStart=35092"
    url2 = "https://investorshub.advfn.com/Cal-Bay-International-Inc-CBYI-5520/?NextStart=35099"
    url3 = "https://investorshub.advfn.com/Cal-Bay-International-Inc-CBYI-5520/?NextStart=1000"
    df1 = get_page(url1)
    df2 = get_page(url2)
    df3 = get_page(url3)
soup = BeautifulSoup(content, "lxml")