鱼C论坛

 找回密码
 立即注册
查看: 1045|回复: 4

[已解决]跪求大佬们

[复制链接]
发表于 2018-3-8 16:18:37 | 显示全部楼层 |阅读模式

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

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

x
Invalid return character or leading space in header: Connection,跪求各位大佬,Python爬虫出现这种错误怎么解决?
最佳答案
2018-3-8 17:12:18
他说你的 header里connection的 keep - alive之间不能有空格
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-3-8 16:32:01 From FishC Mobile | 显示全部楼层
你这样提问神仙也不知道吧,代码发一下啊
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-3-8 16:43:25 | 显示全部楼层
大头目 发表于 2018-3-8 16:32
你这样提问神仙也不知道吧,代码发一下啊

import requests
import csv
import time
import random
import socket
import http.client
from bs4 import BeautifulSoup


def get_content(url,data=None):
    header={
        'Accept': 'text / html, application / xhtml + xml, application / xml;q = 0.9, image / webp, image / apng, * / *;q = 0.8',
        'Accept - Encoding': 'gzip, deflate',
        'Accept - Language': 'zh - CN, zh;q = 0.9',
        'Connection':' keep - alive',
        'User - Agent': 'Mozilla / 5.0(WindowsNT6.1) AppleWebKit / 537.36(KHTML, likeGecko) Chrome / 64.0.3282.186Safari / 537.36'
    }
    timeout = random.choice(range(80,180))
    while True:
        try:
            rep = requests.get(url,headers=header,timeout=timeout)
            rep.encoding = 'utf-8'
            break
        except socket.error as e:
            print('3',e)
            time.sleep(random.choice(range(8,15)))
        except socket.error as e:
            print('4', e)
            time.sleep(random.choice(range(20, 60)))
        except http.client.BadStatusLine as e:
            print('5', e)
            time.sleep(random.choice(range(30, 80)))
        except http.client.IncompleteRead as e:
            print('6', e)
            time.sleep(random.choice(range(5, 15)))

    return rep.text

def get_data(html_text):
    final = []
    bs = BeautifulSoup(html_text,'html.parser')
    body = bs.body
    data =body.find('div',{'id':'7d'})
    ul=data.find('ul')
    li =ul.find_all('li')

    for day in li:
        temp = []
        date = day.find('h1'.string)
        temp.append(date)
        inf = day.find_all('p')
        temp.append(inf[0].string,)
        if inf[1].find('span') is None:
            temperature_highest = None
        else:
            temperature_highest=inf[1].find('span').string
            temperature_highest=temperature_highest.replace('℃','')
        temperature_lowest = inf[1].find('i').string
        temperature_lowest = temperature_lowest.replace('℃','')
        temp.append(temperature_highest)
        temp.append(temperature_lowest)
        final.append(temp)
    return final


def write_data(data,name):
    file_name = name
    with open(file_name,'a',errors='ignore',newline='') as f:
        f_csv = csv.writer(f)
        f_csv.writerows(data)

if __name__=='__main__':
    url = 'http://www.weather.com.cn/weather/101190401.shtml'
    html = get_content(url)
    result = get_data(html)
    write_data(result,'weather.csv')
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-3-8 17:12:18 | 显示全部楼层    本楼为最佳答案   
他说你的 header里connection的 keep - alive之间不能有空格
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2018-3-8 17:36:43 | 显示全部楼层
alltolove 发表于 2018-3-8 17:12
他说你的 header里connection的 keep - alive之间不能有空格

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-3-28 18:58

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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