content = content[content.find('univNameCn:"') + 12:] collegeName = content[:content.find('"')] acollege.append(collegeName)
时间: 2024-06-04 07:06:28 浏览: 105
这段代码的作用是从字符串 `content` 中提取出大学的中文名字,然后将其添加到列表 `acollege` 中。具体来说,代码首先使用 `find` 方法查找字符串 `univNameCn:"` 在 `content` 中第一次出现的位置,然后再加上该字符串的长度 12,可以得到第一个字符为大学名字的位置。接着,使用切片操作将该大学名字提取出来,并将其添加到列表 `acollege` 中。
相关问题
import requests from bs4 import BeautifulSoup from threading import Thread def crawl_books(start, end): session = requests.Session() for i in range(start, end): url = 'http://search.dangdang.com/?key=%BC%C6%CB%E3%BB%FA&act=input&page_index={}'.format(i) try: response = session.get(url, timeout=10) except requests.exceptions.Timeout: print('Timeout occurred when accessing: ' + url) continue page = response.text soup = BeautifulSoup(page, 'lxml') books = soup.find('ul', class_='bigimg') for book in books.find_all('li'): title = book.find('a', class_='pic').get('title') author = book.find('p', class_='search_book_author').text price = book.find('p', class_='price').find('span', class_='search_now_price').text book_link = 'https:' + book.find('a', class_='pic').get('href') try: response = session.get(book_link, timeout=10) except requests.exceptions.Timeout: print('Timeout occurred when accessing: ' + book_link) continue page = response.text soup = BeautifulSoup(page, 'lxml') comment_num_tag = soup.find('a', class_='review_num') if comment_num_tag: comment_num = comment_num_tag.text else: comment_num = '0' print(title, author, price, comment_num) threads = [] for i in range(1, 101, 10): t = Thread(target=crawl_books, args=(i, i+10)) threads.append(t) t.start() for t in threads: t.join()简单优化,使代码能够爬取到每本书籍的评论数量
可以在 `crawl_books` 函数中添加爬取评论数量的代码,如下所示:
```
def crawl_books(start, end):
session = requests.Session()
for i in range(start, end):
url = 'http://search.dangdang.com/?key=%BC%C6%CB%E3%BB%FA&act=input&page_index={}'.format(i)
try:
response = session.get(url, timeout=10)
except requests.exceptions.Timeout:
print('Timeout occurred when accessing: ' + url)
continue
page = response.text
soup = BeautifulSoup(page, 'lxml')
books = soup.find('ul', class_='bigimg')
for book in books.find_all('li'):
title = book.find('a', class_='pic').get('title')
author = book.find('p', class_='search_book_author').text
price = book.find('p', class_='price').find('span', class_='search_now_price').text
book_link = 'https:' + book.find('a', class_='pic').get('href')
try:
response = session.get(book_link, timeout=10)
except requests.exceptions.Timeout:
print('Timeout occurred when accessing: ' + book_link)
continue
page = response.text
soup = BeautifulSoup(page, 'lxml')
comment_num_tag = soup.find('a', class_='review_num')
if comment_num_tag:
comment_num = comment_num_tag.text
else:
comment_num = '0'
print(title, author, price, comment_num)
```
这样就可以爬取到每本书籍的评论数量了。
if self.productname == product[:product.find("Update")]: list_product.append(product) 什么意思
这两行代码是在筛选符合条件的产品名称。具体来说:
- 第一行代码是一个条件语句,判断 `self.productname` 是否与当前 `product` 变量的子字符串相匹配。在这里, `product` 变量是 `list_bios_info` 列表中的一个元素,`product[:product.find("Update")]` 是从 `product` 字符串的开头截取到 "Update" 出现的位置,得到一个子字符串,用于判断是否与 `self.productname` 相等。如果相等,则说明这个产品名称符合条件。
- 第二行代码是将符合条件的产品名称 `product` 添加到 `list_product` 列表中。这里使用的是 `append()` 方法,将当前遍历到的 `product` 变量作为一个元素添加到 `list_product` 列表的末尾。