鱼C论坛

 找回密码
 立即注册
查看: 1108|回复: 3

求教斧正有问题代码

[复制链接]
发表于 2018-1-15 21:31:37 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#爬取出版社名称,并写入到txt文档中
import requests
from bs4 import BeautifulSoup

def open_url(url):
   
#使用代理
    proxies = {"http": "127.0.0.1:1080", "https": "127.0.0.1:1080"}
    headers = {"user-agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.98  Safari/537.36"}
    res = requests.get(url, headers = headers, proxies = proxies)
    return res

def find_data(res):
    soup = BeautifulSoup(res.text, 'html.parser')
    data = soup.select(".list > a")
    return data
           
def main():
    url = "http://www.youlu.net/publisher/"
    res = open_url(url)
   
data = find_data(res)
    with open("E:\\2.txt","w",encoding='utf-8') as f:
        try:
        for i in range(0,len(data)):
            f.write(str(data[i].text)+"\n")
        except Exception as e:
            print("error"+e)
            


if __name__ == "__main__":
    main()
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-1-15 22:53:22 | 显示全部楼层
本帖最后由 8306最硬 于 2018-1-15 23:01 编辑

  1. #爬取出版社名称,并写入到txt文档中
  2. import requests
  3. from bs4 import BeautifulSoup

  4. def open_url(url):
  5.    
  6. #使用代理
  7.     proxies = {"https": "127.0.0.1:1080"}
  8.     headers = {"user-agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.98  Safari/537.36"}
  9.     res = requests.get(url, headers = headers, proxies = proxies)
  10.     return res

  11. def find_data(res):
  12.     soup = BeautifulSoup(res.text, 'html.parser')
  13.     data = soup.select(".list > a")
  14.     return data
  15.             
  16. def main():
  17.     url = "http://www.youlu.net/publisher/"
  18.     res = open_url(url)
  19.    
  20.     data = find_data(res)
  21.     with open("D:\\2.txt","w",encoding='utf-8') as f:
  22.         try:
  23.             for i in range(0,len(data)):
  24.                 f.write(str(data[i].text)+"\n")
  25.         except Exception as e:
  26.             print("error"+e)
  27.             


  28. if __name__ == "__main__":
  29.     main()

复制代码

缩进没缩好,变量名打错了一个,其他代码没问题啊
代理有问题,敢情你打的本机地址代理啊
嗯,你把http的删掉就可以了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-1-15 23:03:16 | 显示全部楼层
8306最硬 发表于 2018-1-15 22:53
缩进没缩好,变量名打错了一个,其他代码没问题啊
代理有问题,敢情你打的本机地址代理啊
...

非常感谢!!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-1-15 23:09:46 | 显示全部楼层

不客气~
原理我也不大清楚,坐等大神来解答
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-25 07:07

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表