写了一个爬虫抓取 并判断的问题求助
class iswitch():def __init__(self):
self.url='http://www.paopaoche.net/psp/114_PSP_'
self.headers={
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36',
#'Referer':"https://www.manhuadb.com/author/370"
}
self.zidian={}
def shuru(self):
gname=input("input you wan search game name:")#这里定义了gname
def main(self):
for p in range(1,10):
r =requests.get(self.url+str(p)+'.html',self.headers)
r.encoding="gbk"
soup = BeautifulSoup(r.text,'lxml')
soup2=soup.find('p',class_="list clearfix")
#print(soup2)
for j in soup2.find_all('u'):
#print(j.getText())
self.zidian["name"]=j.getText()
if self.shuru().gname in self.zidian["name"]:#想在这里将gname 放到循环的字典里判断是否出现我搜索的游戏名字,然后搜索到了 打印出来,但是这里 报错,说gname是空值 应该怎么定义?
print(self.zidian["name"])
if __name__ == "__main__":
x = iswitch()
x.main() 弱弱的问一句 你执行以后有显示input you wan search game name这个让你输入吗 {:301_998:} 这是在用流程控制的思路写面向对象的类,思路的起点就错了……
gname这个变量不是 iswitch 这个类的属性,调用肯定出问题 class iswitch():
def __init__(self):
self.url = 'http://www.paopaoche.net/psp/114_PSP_'
self.headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36',
# 'Referer':"https://www.manhuadb.com/author/370"
}
self.zidian = {}
def main(self):
gname = input("input you wan search game name:")# 这里定义了gname
for p in range(1, 10):
r = requests.get(self.url + str(p) + '.html', self.headers)
r.encoding = "gbk"
soup = BeautifulSoup(r.text, 'lxml')
soup2 = soup.find('p', class_="list clearfix")
# print(soup2)
for j in soup2.find_all('u'):
# print(j.getText())
self.zidian["name"] = j.getText()
if gname in self.zidian["name"]:
print(self.zidian["name"])
rsnodame 发表于 2020-9-9 11:20
这是在用流程控制的思路写面向对象的类,思路的起点就错了……
gname这个变量不是 iswitch 这 ...
我看着一直怪怪的,一语道破哈哈 感觉是作用域的问题 superain 发表于 2020-9-9 11:11
弱弱的问一句 你执行以后有显示input you wan search game name这个让你输入吗
你还别说 跳出来了。。。。就是输入了报错的 import requests
from bs4 import BeautifulSoup
url='https://s.paopaoche.net/'
headers={
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36',
}
plantform = ('android','apple','pc','zj','xcx','vr')
gname=input("请输入游戏名称:")
plantform_numb = input('输入下载平台(安卓请输入0,苹果请输入1,PC请输入2,主机请输入3,小程序请输入4,VR请输入5):') or '0'
cid = plantform
params ={
'cid':cid,
'k':gname
}
r_page = requests.get(url,params = params, headers = headers)
soup = BeautifulSoup(r_page.text, 'lxml')
result = soup.find('dl',attrs={'id':"result"})
for child in result.children:
if not child.string:
temp_name_version = child.div.a.text
temp_name,temp_version = temp_name_version.split('下载')
name = temp_name.strip()
version = temp_version.strip()
url = child.div.a['href']
print(name,version,url)
页:
[1]