Форум сайта python.su
помогите пожалуйста понять один очень важный момент, чтение документации не помогает(
не совсем понимаю как работает метод contents. следующим скриптом я делаю парсинг сайта
import urllib.request import urllib.parse import re import os import requests import bs4 beginIndex = 1000 endIndex = 1010 prefix = "http://www.inpic.ru" rep_chars = ['\\', '/', ':', '*', '?', '"', '<', '>', '|'] for i in range(beginIndex, endIndex): req = requests.get(prefix + '/image/' + str(i)) if req.status_code == requests.codes.ok: print(i, '\t', req.status_code, '\t', req, end='\n') soup = bs4.BeautifulSoup(req.content) #print(soup.prettify()) name = soup.find("td", {"class": "post_title"}).contents[1].contents author = soup.find("td", {"class": "post_title"}).contents[2].contents[1].contents print(name[0]) mainImagePath = soup.find("img", {"class": "image last"})["src"] manyImages = soup.findAll("img", {"class": "image"}) print(mainImagePath) print(manyImages) if manyImages != []: newName = re.escape(name[0]) #newName = re.sub('[\\\\/:*?"<>|]', 'special char', name[0]) #for char in rep_chars: #newName = name[0].replace(char, '') try: os.mkdir(str(newName)) except FileExistsError: print('FileExistsError') except PermissionError : print('PermissionError ') except : print('error') else: print('ok') else: pass
<table> <tbody><tr> <td> <table class="post_rating"> <tbody><tr> <td id="post_rating_left1001" class="post_rating_left_positive"> <div class="post_rating_none"></div> </td> <td id="post_rating_center1001" class="post_rating_value_positive"> <span id="post_rating1001">19</span> </td> <td id="post_rating_right1001" class="post_rating_right_positive"> <div class="post_rating_none"></div> </td> </tr> </tbody></table> </td> <td class="post_title"> <h1>Эти хитрые японцы...</h1> <div class="date_author"><a href="/users/Stack/">Stack</a> / 28.06.2008 21:59</div> </td> </tr> </tbody></table>
<h1>Эти хитрые японцы...</h1>
<a href="/users/Stack/">Stack</a>
<a href="/users/Stack/">Stack</a>
Traceback (most recent call last):
File “C:\VINT\OPENSERVER\OpenServer\domains\localhost\python\parse_html\1\q.py”, line 21, in <module>
author = soup.find(“td”, {“class”: “post_title”}).contents.contents.contents
File “C:\Python33\lib\site-packages\bs4\element.py”, line 675, in __getattr__
self.__class__.__name__, attr))
AttributeError: ‘NavigableString’ object has no attribute ‘contents’
Отредактировано zlodiak (Фев. 18, 2014 19:43:57)
Офлайн