鱼C论坛

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

爬房价表,正则表达式group函数报错

[复制链接]
发表于 2018-9-25 18:30:08 | 显示全部楼层 |阅读模式

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

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

x
import requests
import bs4
import re
import openpyxl
def open_url(url):
    headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36'}

    res = requests.get(url,headers=headers)
    return res
def find_word(res):
    soup = bs4.BeautifulSoup(res.text,'lxml')
    content = soup.find("article",id="mp-editor")
    targets = content.find_all("td")
    targets = iter(targets)

    data = []
    for each in targets:
        if each.text.isnumeric():
            data.append([
                re.search(r'<td>(\d.*)</td>',next(targets).text).group(1),
                re.search(r'<td>(\w.*)</td>',next(targets).text).group(),
                re.search(r'<td>(\d.*)</td>',next(targets).text).group(),
                re.search(r'<td>(\w.*)</td>',next(targets).text).group(),
                re.search(r'<td>(\w.*)</td>',next(targets).text).group(),
                re.search(r'<td>(\w.*)</td>',next(targets).text).group(),
                re.search(r'<td>(\w.*)</td>',next(targets).text).group()
            ])
    return data

def save_exal(data):
    wb = openpyxl.Workbook()
    wb.guess_types = True
    ws = wb.active
    ws.append("排名","城市","中位房价","级别","省份","市场等级","区域")
    for each in data:
        ws.append(each)
    wb.save("2018房价工资比.xlsx")

def main():
    host = 'https://www.sohu.com/a/230135374_119746'
    res = open_url(host)
    data = find_word(res)


    save_exal(data)

if __name__ =="__main__":
    main()

报错信息如下:
Traceback (most recent call last):
  File "E:/untitled/2018房价工资比.py", line 62, in <module>
    main()
  File "E:/untitled/2018房价工资比.py", line 55, in main
    data = find_word(res)
  File "E:/untitled/2018房价工资比.py", line 23, in find_word
    re.search(r'<td>(\d.*)</td>',next(targets).text).group(1),
AttributeError: 'NoneType' object has no attribute 'group'


哪位大佬可以帮看一下??



小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2018-9-25 18:33:31 | 显示全部楼层
正则不对,没捕获到
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-9-25 19:01:35 | 显示全部楼层
重点不是正则,是你用了迭代器,
for each in targets:
        if each.text.isnumeric():
            data.append([
                re.search(r'<td>(\d.*)</td>',next(targets).text).group(1),
each.text是1时。next(targets).text就是北京,北京肯定不是数字
targets本身被迭代的时候你又用了迭代方法
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-9-25 19:17:30 | 显示全部楼层
本帖最后由 塔利班 于 2018-9-25 19:19 编辑
  1. def find_word(res):
  2.     soup = bs4.BeautifulSoup(res.text,'lxml')
  3.     content = soup.find("article",id="mp-editor")
  4.     targets = content.find_all("tr")
  5.     data=[e.text.split('\n')[1:-1] for e in targets[1:]]
  6.     #print(data)
  7.     return data
复制代码

这么改下
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-10-2 21:11:20 | 显示全部楼层

哈哈,谢谢,已经解决了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-1-2 10:35

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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