从get_page_data
功能转到for
循环以提取商家名称,地址等时,出现Nonetype
错误的问题。我现在知道这意味着值None
正在传递到for
循环中,但我不确定导致此错误的是什么:\ n
AttributeError: 'NoneType' object has no attribute 'text'
#!/opt/local/bin/python
import requests
import re
from bs4 import BeautifulSoup
import csv
#Read csv
with open ("gyms4.csv") as file:
reader = csv.reader(file)
csvfilelist = [row[0] for row in reader]
#Get data from each url
def get_page_data():
for page_data in csvfilelist:
r = requests.get(page_data.strip())
soup = BeautifulSoup(r.text, 'html.parser')
yield soup
#Complete work on data
for page in get_page_data():
name = page.find("span",{"class":"wlt_shortcode_TITLE"}).text
address = page.find("span",{"class":"wlt_shortcode_map_location"}).text
phoneNum = page.find("span",{"class":"wlt_shortcode_phoneNum"}).text
email = page.find("span",{"class":"wlt_shortcode_EMAIL"}).text
th = pages.find('b',text="Category")
td = th.findNext()
for link in td.findAll('a',href=True):
match = re.search(r'http://(\w+).(\w+).(\w+)', link.text)
if match:
web_address = link.text
gyms = [name,address,phoneNum,email,web_address]
gyms.append(gyms)
#Saving specific listing data to csv
with open ("xgyms.csv", "w") as file:
writer = csv.writer(file)
for row in gyms:
writer.writerow([row])