使用python写一段爬取网站httpscatalogueite-exporuen-GBexhibitorlistaspxproject_id=489下面的列表数据中的每一条的电话和官网的信息的代码
以下是使用Python爬取https://catalogue.ite-expo.ru/en-GB/exhibitorlist.aspx?project_id=489网站中的电话和官网信息的代码:
import requests
from bs4 import BeautifulSoup
url = 'https://catalogue.ite-expo.ru/en-GB/exhibitorlist.aspx?project_id=489'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
table = soup.find('table', {'id': 'exhibitorlist_tbl'})
for row in table.find_all('tr')[1:]:
columns = row.find_all('td')
name = columns[0].text.strip()
phone = columns[2].text.strip()
website = columns[3].find('a')['href'] if columns[3].find('a') else ''
print(f'{name}: {phone} {website}')
该代码首先使用requests库获取网页的HTML代码,然后使用BeautifulSoup库解析HTML代码。接着,找到网页中的展商列表表格,并遍历表格中的每一行。对于每一行,获取展商名称、电话和官网信息,并打印出来。
需要注意的是,有些展商可能没有官网信息,因此在获取官网信息时需要进行判断。
原文地址: https://www.cveoy.top/t/topic/bWuO 著作权归作者所有。请勿转载和采集!