![](/img/trans.png)
[英]Webscraping using Python Beautifulsoup loop through HTML tags
[英]Webscraping with BeautifulSoup in Python tags
我目前正在尝试从以下链接中获取一些信息:
我想在 Python 中使用 BeautifulSoup 抓取表中的一些信息。 理想情况下,我想将表格中的“Groupo Parliamentario”、“Titulo”、“Sumilla”和“Autores”作为单独的项目刮掉。
到目前为止,我已经使用 BeautifulSoup 开发了以下代码:
from bs4 import BeautifulSoup
import requests
import pandas as pd
url = 'http://www2.congreso.gob.pe/Sicr/TraDocEstProc/CLProLey2001.nsf/ee3e4953228bd84705256dcd008385e7/4ec9c3be3fc593e2052571c40071de75?OpenDocument'
page = requests.get(url)
soup = BeautifulSoup(page.text, 'html.parser')
table = soup.find('table', {'bordercolor' : '#6583A0'})
contents = []
summary = []
authors = []
contents.append(table.findAll('font'))
authors.append(table.findAll('a'))
我正在苦苦挣扎的是,抓取作者的代码只会抓取列表中的第一作者。 理想情况下,我需要抓取列表中的所有作者。 这对我来说似乎很奇怪,因为查看网页的 html 代码,列表中的所有作者都用'<a href = >'
标签表示。 我认为table.findAll('a'))
会抓住列表中的所有作者。
最后,我只是将非常混乱的 html (标题、摘要、议会组)的 rest 全部转储到contents
下的一个长字符串中。 我不确定我是否遗漏了什么,我对 html 和网络抓取有点陌生,但有没有办法将这些项目拉出来并单独存储(即:仅将标题存储在 object 中,只是object 等中的摘要)。 我很难在 web 页面的代码中识别唯一标签来执行此操作。 或者这是我应该在抓取后清理和解析的东西?
要获得可以使用的作者:
soup.find('input', {'name': 'NomCongre'})['value']
output:
'Santa María Calderón Luis,Alva Castro Luis,Armas Vela Carlos,Cabanillas Bustamante Mercedes,Carrasco Távara José,De la Mata Fernández Judith,De La Puente Haya Elvira,Del Castillo Gálvez Jorge,Delgado Nuñez Del Arco José,Gasco Bravo Luis,Gonzales Posada Eyzaguirre Luis,León Flores Rosa Marina,Noriega Toledo Víctor,Pastor Valdivieso Aurelio,Peralta Cruz Jonhy,Zumaeta Flores César'
刮掉Grupo Parlamentario
table.find_all('td', {'width': 446})[1].text
output:
'Célula Parlamentaria Aprista'
to scrape Título
:
table.find_all('td', {'width': 446})[2].text
output:
'IGV/SELECTIVO:D.L.821/LEY INTERPRETATIVA '
刮Sumilla
:
table.find_all('td', {'width': 446})[3].text
output:
' Propone la aprobación de una Ley Interpretativa del Texto Original del Numeral 1 del Apéndice II del Decreto Legislativo N°821,modificatorio del Texto Vigente del Numeral 1 del Apéndice II del Texto Único Ordenado de la Ley del Impuesto General a las Ventas y Selectivo al Consumo,aprobado por Decreto Supremo N°054-99-EF. '
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.