我如何从这个网站获得大使馆和电子邮件的列表?

2024-06-16 11:22:01 发布

您现在位置:Python中文网/ 问答频道 /正文

假设我想从这个列表中得到大使馆的名字和电子邮件:

https://www.embassy-worldwide.com/country/united-states/

我该怎么做


Tags: httpscom列表电子邮件www名字countryworldwide
1条回答
网友
1楼 · 发布于 2024-06-16 11:22:01

这应该让你开始,使用下面的代码从网站列表中获取大使馆名称和国家名称。理解下面的代码并尝试提取电子邮件

from lxml import html
import requests
page = requests.get('https://www.embassy-worldwide.com/country/united-states/')
tree = html.fromstring(page.content)
country = tree.xpath('//div[@class="posts-container col-md-6"]/h2/text()')
embassy = tree.xpath('//div[@class="posts-container col-md-6"]/ul/li/a/text()')
print(country)
print(embessy)

输出:

country:
['Belgium',
 'Afghanistan',
 'Albania',
 'Andorra',
 'Algeria',
 'Antigua & Barbuda',
 'Angola',...]

embessy:
['Honorary Consulate of Belgium in Phoenix',
 'Consulate General of Armenia in Los Angeles',
 'Permanent Mission of Afghanistan to United Nations',
 'Consulate General of Afghanistan in Los',...]

see this image

相关问题 更多 >