weixin_43884285
2019-01-03 18:08用python3爬链家的网页,遇到了AttributeError: 'str' object has no attribute 'select'的报错?
源码:
# -*- coding:utf-8 -*-
from urllib import request
from bs4 import BeautifulSoup
import bs4
def get_data():
url = 'https://nj.lianjia.com/ershoufang/gulou/'
headers = {
'User_agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36'
}
req = request.Request(url, headers=headers)
response = request.urlopen(req)
if response.getcode() == 200:
data = response.read()
data = str(data, encoding='utf-8')
print(data)
with open('index.html', mode='w', encoding='utf-8') as f:
f.write(data)
def parse_data():
with open('index.html', mode='r', encoding='utf-8') as f:
html = f.read()
bs = BeautifulSoup(html, 'html.parser') # 析HTML文件的时候就是用parser的
divs = bs.find_all(class_='clear LOGCLICKDATA')[0].get_text()
for div in divs[0:]:
div1 = div.select('.address')[0]
print(div1)
if __name__ == '__main__':
# get_data()
parse_data()
报错显示是
div1 = div.select('.address')[0]
这一行出了问题
还请各位帮忙看看是怎么了
- 点赞
- 回答
- 收藏
- 复制链接分享
3条回答
为你推荐
- AttributeError: 'function' object has no attribute 'cursor'?
- python
- sql
- 2个回答
- AttributeError: 'Ship' object has no attribute 'blitme'报错,找了半天了还是不行,新手太难了
- python
- 3个回答
- 关于VSCode里运行Python报错Module 'cv2' has no 'xxx' member的问题
- python
- 3个回答
- AttributeError: 'DataFrame' object has no attribute 'itemtype'
- python
- 3个回答
- Python 创建Dog类时 报错AttributeError: 'Dog' object has no attribute 'sit'
- python
- 2个回答
换一换