设为首页收藏本站

Crossin的编程教室

 找回密码
 立即加入
查看: 4293|回复: 1
打印 上一主题 下一主题

新人求助,为什么pycharm代码正常运行了没有返回结果。。。

[复制链接]

1

主题

0

好友

5

积分

新手上路

Rank: 1

跳转到指定楼层
楼主
发表于 2018-1-22 17:32:48 |只看该作者 |倒序浏览
import json
from urllib.parse import urlencode
import requests
from requests import RequestException

def get_page_index(keyword):
    data={
        
'format': 'json',
        
'offset': 0,
        
'keyword': 'keyword',
        
'autoload': 'true',
        
'count': '20',
        
'cur_tab': 3,
        
'from':'gallery'
    }
    url =
'https://www.toutiao.com/search_content/?'+urlencode(data)
    response = requests.get(url)
   
try:
        
if response.status_code == 200:
            
return response.text
        
else:
            
return None

    except RequestException:
        
return None

def qwer(html):
    data = json.loads(html)
   
try:
        
if data and "data" in data.keys():
            
for itme in data.get('data'):
               
yield itme.get('article_url')
   
except:
        
print('这里失败了')


def main():
    html=get_page_index(
"红旗渠")
   
for url in qwer(html):
        
print(url)



if __name__ == '__main__':
    main()
Process finished with exit code 0这条百度了下说是程序正常运行了,没什么没有url的返回啊。。。求大神帮助。。





回复

使用道具 举报

0

主题

0

好友

137

积分

注册会员

Rank: 2

沙发
发表于 2018-1-23 17:32:45 |只看该作者
你的url参数传的顺序有问题,
data={
        'offset':0,
        'format':'json',
        'keyword':keyword,
        'autoload':'true',
        'count':'20',
        'cur_tab':'3',
        'from':'gallery'
        }
这样传就ok了,否则你构造的网站都不存在,return None 肯定没有url的返回。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即加入

QQ|手机版|Archiver|Crossin的编程教室 ( 苏ICP备15063769号  

GMT+8, 2024-4-20 01:24 , Processed in 0.025561 second(s), 21 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部