html - 如何使用beautifulsoup从html标签中获取链接部分
问题描述
我正在尝试从网站中提取显示文本和相应的超链接。
from bs4 import BeautifulSoup
import requests
import re
import csv
source = requests.get('https://eci.gov.in/files/category/1359-general-election-2019/').text
soup = BeautifulSoup(source, 'lxml')
container = soup.find_all(class_="ipsType_break ipsContained")
for data in container:
print(f'{data.a.text}\n')
这里data
包含
<a href="https://eci.gov.in/files/file/11007-35-vellore-pc-result-election-was-held-in-august-2019/" title="View the file 35. VELLORE PC RESULT (ELECTION WAS HELD IN AUGUST 2019) ">35. VELLORE PC RESULT (ELECTION WAS HELD IN AUGUST 2019)</a>
data.a.text
给我显示文本 ( 35. VELLORE PC RESULT (ELECTION WAS HELD IN AUGUST 2019)
) 但我无法提取链接部分 ( https://eci.gov.in/files/file/11007-35-vellore-pc-result-election-was-held-in-august-2019/
)。我只想使用上面的代码结构,但我不确定如何访问相关标签。
解决方案
要访问该href
属性,只需执行data.a["href"]
. 例如:
import requests
from bs4 import BeautifulSoup
source = requests.get(
"https://eci.gov.in/files/category/1359-general-election-2019/"
).text
soup = BeautifulSoup(source, "lxml")
container = soup.find_all(class_="ipsType_break ipsContained")
for data in container:
print(data.a.get_text(strip=True))
print(data.a["href"]) # <-- to access the 'href' attribute
print("-" * 80)
印刷:
35. VELLORE PC RESULT (ELECTION WAS HELD IN AUGUST 2019)
https://eci.gov.in/files/file/11007-35-vellore-pc-result-election-was-held-in-august-2019/
--------------------------------------------------------------------------------
1.The Schedule of General Election to Lok Sabha, 2019
https://eci.gov.in/files/file/10993-1the-schedule-of-general-election-to-lok-sabha-2019/
--------------------------------------------------------------------------------
2. HIGHLIGHTS
https://eci.gov.in/files/file/10991-2-highlights/
--------------------------------------------------------------------------------
3. List Of Political Parties Participated
https://eci.gov.in/files/file/10989-3-list-of-political-parties-participated/
--------------------------------------------------------------------------------
4. List Of Successful Candidate
https://eci.gov.in/files/file/10987-4-list-of-successful-candidate/
--------------------------------------------------------------------------------
5. Number And Types Of Constituencies
https://eci.gov.in/files/file/10985-5-number-and-types-of-constituencies/
--------------------------------------------------------------------------------
6. State Wise Candidate data Summary
https://eci.gov.in/files/file/10983-6-state-wise-candidate-data-summary/
--------------------------------------------------------------------------------
7. Constituency (PC) wise summary
https://eci.gov.in/files/file/10981-7-constituency-pc-wise-summary/
--------------------------------------------------------------------------------
8. Number of Candidates Per Constituency
https://eci.gov.in/files/file/10979-8-number-of-candidates-per-constituency/
--------------------------------------------------------------------------------
9.State Wise Number Of Electors
https://eci.gov.in/files/file/10977-9state-wise-number-of-electors/
--------------------------------------------------------------------------------
10. VOTERS INFORMATION
https://eci.gov.in/files/file/10975-10-voters-information/
--------------------------------------------------------------------------------
11. State Wise Participation of Overseas Electors Voters
https://eci.gov.in/files/file/10973-11-state-wise-participation-of-overseas-electors-voters/
--------------------------------------------------------------------------------
12. State Wise Voters Turn Out
https://eci.gov.in/files/file/10971-12-state-wise-voters-turn-out/
--------------------------------------------------------------------------------
13. PC Wise Voters Turn Out
https://eci.gov.in/files/file/10969-13-pc-wise-voters-turn-out/
--------------------------------------------------------------------------------
14. PC Wise Distribution Of Votes Polled
https://eci.gov.in/files/file/10967-14-pc-wise-distribution-of-votes-polled/
--------------------------------------------------------------------------------
15. Assembly Segment Wise Information Electors
https://eci.gov.in/files/file/10965-15-assembly-segment-wise-information-electors/
--------------------------------------------------------------------------------
16. Details of Re-poll Held
https://eci.gov.in/files/file/10963-16-details-of-re-poll-held/
--------------------------------------------------------------------------------
17.State Wise Seat Won & Valid Votes Polled by Political Parties
https://eci.gov.in/files/file/10961-17state-wise-seat-won-valid-votes-polled-by-political-parties/
--------------------------------------------------------------------------------
18. Partywise Seat Won Valid Votes Polled in Each State
https://eci.gov.in/files/file/10959-18-partywise-seat-won-valid-votes-polled-in-each-state/
--------------------------------------------------------------------------------
19.Political Party Wise Deposit Forfeited
https://eci.gov.in/files/file/10957-19political-party-wise-deposit-forfeited/
--------------------------------------------------------------------------------
20. Performance of National Parties
https://eci.gov.in/files/file/10955-20-performance-of-national-parties/
--------------------------------------------------------------------------------
21.Performance of State Parties
https://eci.gov.in/files/file/10953-21performance-of-state-parties/
--------------------------------------------------------------------------------
22.PERFORMANCE OF REGISTERED (UNRECOGNISED) PARTIES
https://eci.gov.in/files/file/10951-22performance-of-registered-unrecognised-parties/
--------------------------------------------------------------------------------
23. Participation of Women Electors in Poll
https://eci.gov.in/files/file/10949-23-participation-of-women-electors-in-poll/
--------------------------------------------------------------------------------
24. Participation of Women Candidates in Poll
https://eci.gov.in/files/file/10947-24-participation-of-women-candidates-in-poll/
--------------------------------------------------------------------------------
Reiterating Guidelines for strict observance of Covid Protocols during election rallies, meetings, campaigning, etc- reg
https://eci.gov.in/files/file/13293-reiterating-guidelines-for-strict-observance-of-covid-protocols-during-election-rallies-meetings-campaigning-etc-reg/
--------------------------------------------------------------------------------
推荐阅读
- oracle - 使用 pl/sql 或 sql 将数据拆分到学生表中的多个列
- clang-format - 尽可能保持参数/参数在同一行
- php - 如何检查我的工作是否在分派后调用了句柄方法?
- spring-boot - redis:使用来自多个服务实例的 zset 中的元素
- python - 如何将 spark.sql.dataframe 写入数据块中的 S3 存储桶?
- swift - 列表仅显示 Swift 中传递的数据中的最后一个数组
- autodesk-forge - 使用选项 setDisplayEdges(true) 时材质变为白色
- android - 如何在 jsoup 的 ui li 标签中获取文本?
- javascript - 遍历段落数组并从 JavaScript 中的另一个数组添加内容
- javascript - 当我使用 db.Entry(x).State = EntityState.Modified; 时,Json 结果不起作用