求1-100以内的素数
思路:
直接求素数的思路当时没想好 ,就直接排除法将不是素数的从列表中删除
# -*- coding: utf-8 -*-
def is_prime():
p = [i for i in range(1, 100)]
for n in range(2, 100):
for m in range(2, n):
if n % m == 0:
p.remove(n)
break
print p
if __name__ == "__main__":
is_prime()
爬取糗事百科页面
思路:
糗百的需要爬取的信息都是在页面的源码里,直接获取html的提取数据,主要是BeautifulSoup的用法的熟悉
# -*- coding:utf-8 -*-
import requests
from bs4 import BeautifulSoup
import re
url = 'http://www.qiushibaike.com/text/'
headers = {
"User-Agent": 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36',
"Connection": 'keep-alive',
"Accept": 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8'
}
html = requests.get(url, headers=headers).content
soup = BeautifulSoup(html, 'html.parser')
for qsbk in soup.find_all(name='div', class_='article block untagged mb15'):
Author = qsbk.find(name='h2').text
#匿名用户性别、年龄无法提取,此次需加判断是否是None
Gender = qsbk.find(name='div', class_=re.compile("articleGender .*"))
if Gender is not None:
#attrs["xx"]方法是获取标签属性的方法,此处获取列表index为1的字符串,然后切片得到性别元素
Gender = Gender.attrs["class"][1][:-4]
else:
Gender = None
Age = qsbk.find(name='div', class_=re.compile("articleGender .*"))
if Age is not None:
Age = Age.text.strip()
else:
Age = None
State_vote = qsbk.find(name='i', class_=re.compile("number")).text
State_comments = qsbk.find(name='div', class_=re.compile("stats")).text.split()[-2]
Content = qsbk.find(name='div', class_='content').text.strip()
print Author, Gender, Age,Content,State_vote,State_comments
简书首页文章信息
熟悉Xpath的用法 ,尽量将路径写详细
# -*- coding:utf-8 -*-
import requests
from lxml import etree
url = 'http://www.jianshu.com'
html = requests.get(url).content
#print html
selector = etree.HTML(html)
notes = selector.xpath('//*[starts-with(@id, "note-")]/div')
print len(notes)
for note in notes:
title = note.xpath('a[@class="title"]/text()')[0]
author = note.xpath('div[1]/div/a[@class="blue-link"]/text()')[0]
time = note.xpath('div[1]/div/span[@class="time"]/@data-shared-at')[0].split("+")[0].replace("T", " ")
topic = note.xpath('div[2]/a[@class="collection-tag"]/text()')[0]
read_count = note.xpath('div[@class="meta"]/a[2]/text()')[-1].strip()
comments_count = note.xpath('div[@class="meta"]/a[3]/text()')[-1].strip()
like_count = note.xpath('div[@class="meta"]/span[1]/text()')[-1].strip()
money = note.xpath('div[@class="meta"]/span[2]/text()')
if len(money) > 0:
money = money[-1].strip()
else:
money = 0
print title, author, time, topic, read_count, comments_count, like_count, money