Python 当某些属性为';不适用于所有页面
我正在尝试从一个名为knowyourcity.info的网站上浏览网页,该网站上有许多关于信息的定居点。这是我当前的循环:Python 当某些属性为';不适用于所有页面,python,beautifulsoup,Python,Beautifulsoup,我正在尝试从一个名为knowyourcity.info的网站上浏览网页,该网站上有许多关于信息的定居点。这是我当前的循环: for u in urllist: response = get(u) html_soup = BeautifulSoup(response.text, "html.parser") headers_containers = html_soup.find('div', class_ = 'settlement-base-statu
for u in urllist:
response = get(u)
html_soup = BeautifulSoup(response.text, "html.parser")
headers_containers = html_soup.find('div', class_ = 'settlement-base-status section text-center')
names = headers_containers.h2.text
name.append(names)
year_established = headers_containers.h3.text
year.append(year_established)
headers1_containers = html_soup.find('div', class_ = 'col-xs-12 text-center')
countries = headers1_containers.h4.a.text
country.append(countries)
headers2_containers = html_soup.find('div', class_ = 'bold-it', id = "population")
populations = headers2_containers.text
population.append(populations)
headers3_containers = html_soup.find('div', class_ ='bold-it', id='sharedTaps')
tap = headers3_containers.text
taps.append(tap)
headers4_containers = html_soup.find_all('div', class_ = 'bold-it')
toiletSeat_toPerson = headers4_containers[7].text
toiletsToPerson.append(toiletSeat_toPerson)
但是,对于某些定居点,某些属性不可用。如何向该循环添加“if true”语句?如果要按条件跳过循环周期,可以使用
continue
关键字
for url in urllist:
if condition:
continue
如果条件
为真,将中断当前循环周期
然后从
urlist
中的下一个url开始,您可以共享您试图从中删除的url吗?有时BeautifulSoup无法获取数据。因此,尝试Selenium web驱动程序。如果:,您是否尝试编写?你试着那样做的时候有什么问题?