Python 如何从父标记中读取两个子标记的值
有没有办法为Python 如何从父标记中读取两个子标记的值,python,python-3.x,beautifulsoup,Python,Python 3.x,Beautifulsoup,有没有办法为中嵌入的文本创建两个变量1)“信息头”和2)信息详细信息(不包括 例如,信息标题=场馆 例如,信息详情=邵氏中心斯科茨路1号美国商会办公室#23-03 S(228208)-强生礼堂 for link in final_urls[:1]: webpage_response = requests.get(link) event = BeautifulSoup(webpage_response.content, "html.parser") title = even
中嵌入的文本创建两个变量1)“信息头”和2)信息详细信息(不包括
例如,信息标题=场馆
例如,信息详情=邵氏中心斯科茨路1号美国商会办公室#23-03 S(228208)-强生礼堂
for link in final_urls[:1]:
webpage_response = requests.get(link)
event = BeautifulSoup(webpage_response.content, "html.parser")
title = event.find("h1").get_text()
name = event.find("p", attrs={"class":"name"}).get_text()
event_information = event.find("div", attrs={"class":"info"})
raw_text = event_information.find_all("p")
print(raw_text)
[地点:邵氏中心斯科茨路1号美国商会办公室#23-03 S(228208)-强生礼堂,日期:2019年7月9日,时间:上午11:45-下午1:30,价格:25.00美元]
for link in final_urls[:1]:
webpage_response = requests.get(link)
event = BeautifulSoup(webpage_response.content, "html.parser")
title = event.find("h1").get_text()
name = event.find("p", attrs={"class":"name"}).get_text()
event_information = event.find("div", attrs={"class":"info"})
raw_text = event_information.find_all("p")
print(raw_text)
选择类分隔符后,您可以使用下一个同级
,因为这将使您超越:
我将使用一个示例事件和错误处理进行演示
import requests
from bs4 import BeautifulSoup as bs
r = requests.get('https://www.amcham.org.sg/event/8914/')
soup = bs(r.content, 'lxml')
information_header = soup.select_one('.label')
information_detail = soup.select_one('.divider')
if information_header is None:
information_header = 'Not listed'
else:
information_header = information_header.text
try:
information_detail = information_detail.next_sibling
except:
information_detail = 'Not listed'
@Jeffersong当我查看文本时,我看到信息存储在
标记中。在这些
标签中,信息以:
格式存储。因此,我使用str.split(':',1)
方法拆分字符串(注意1-这意味着我只想拆分一次)。这个拆分字符串的第一个元素是header,第二个元素是我们的info。最后一行是如何工作的?“{:@jeffersong这是字符串格式语法添加冒号你可以这样做,例如print({:你可以共享url吗?是的。
Venue AmCham Office, 1 Scotts Rd, Shaw Centre #23-03 S(228208) - J&J Auditorium
Date July 09, 2019
Time 11:45 AM - 1:30 PM
Price $25.00
import requests
from bs4 import BeautifulSoup as bs
r = requests.get('https://www.amcham.org.sg/event/8914/')
soup = bs(r.content, 'lxml')
information_header = soup.select_one('.label')
information_detail = soup.select_one('.divider')
if information_header is None:
information_header = 'Not listed'
else:
information_header = information_header.text
try:
information_detail = information_detail.next_sibling
except:
information_detail = 'Not listed'