Hexo新建文章并发布
- 进入博客目录,在/source/_posts文件夹下直接建立一个xx.md文件
- hexo generate #生成更改
- hexo server #本地预览生成后的页面
- hexo deploy #将生成的更改部署到GitHub上(自己域名下展示有些许延迟,可以先去GitHub上看代码是否成功)
免费(需要翻墙,免费的不稳)
http://www.gatherproxy.com/zh/
收费(6块钱,1000个)
http://www.mogumiao.com/buy
data = requests.get(test_url)
html = data.content
html_doc = str(html, 'utf-8')
具体问题:图片地址被包含在 data-original 或 src 属性中
xpath1 | xpath2
wb 二进制方式写入
r = requests.get(img_path, stream=True)
time.sleep(1)
if r.status_code == 200:
with open(base_path.format(name), 'wb') as f:
for chunk in r.iter_content(1024):
f.write(chunk)
ids = list(set(ids))
# 示例
<a class="nickname" target="_blank" href="/u/82df3ebfe1d0">顾梦辰</a>
# solution
username_list = re.findall('<a class="nickname" target="_blank"
href=(.*?)</a>)', source)
for username in username_list:
username = username.split('>')[1]
print(username)
def low_work(t, s):
print("%d %d" % (t, s))
time.sleep(t)
if __name__ == '__main__':
pool = Pool(4)
tasks = [(x, y) for x in range(4,8) for y in range(10, 14)]
pool.starmap(low_work, tasks)
pool.close()
返回值具有两种类型 dict 和 list
对于动态调接口生成data然后通过 jquery 生成 html 的只能通过找相应的数据接口来实现爬虫
因为requests请求下来的没有html(根本原因是没有调生成html的js)
首先检查是否安装了,检验方式是在命令行中写 demo 执行 import 指定包查看是否成功
其次 pycharm 中检查 project interpreter 设置路径是否有问题
命令行创建项目 scrapy startproject projectname
然后通过pycharm open文件夹打开进行具体内容编写
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_1)
AppleWebKit/537.36 (KHTML, like Gecko) ''Chrome/63.0.3239.132 Safari/537.36'}
str_data = str(response.text)
str_data.encode('utf-8').decode('unicode_escape')
files.xpath('//i[@class="iconfont ic-list-read"]/following::text()[1]')
soup = BeautifulSoup(text, 'html.parser')
print(soup.prettify())
它使用的逻辑是 A in B
# 需求爬取el数据
# 错误示例
# el_list = el_list_outer[0].find_all(name='div', attrs={"class": "el"})
# <div class="el mk" 也会被删选出来
# 正确方法 使用lambda
# el_list = el_list_outer[0].find_all(lambda tag: tag.get('class') == ['el'])
introduction = introduction_div[0].xpath('string(.)'
from multiprocessing import Pool, Manager
# 在文件开头通过Manager获取result_list
result_list = Manager().list()
if __name__ == '__main__':
# 使用多进程
# 进程共享数据
build_all_url()
pool = Pool(4)
# 在get_info函数中执行result_list.addpend(data) 操作
pool.map(get_info, url_list)
pool.close()
pool.join()
node_list = introduction_div[0].xpath('node()')
for child in el_list_outer.children:
if type(child) == bs4.element.Tag:
if child.name != 'div':
introduction = introduction + child.get_text()
概述:获取其上一级标签做切片
具体问题:
<a href="/films/9025" title="喜剧之王" class="image-link" data-act="boarditem-click" data-val="{movieId:9025}">
<img src="//ms0.meituan.net/mywww/image/loading_2.e3d934bf.png" alt="" class="poster-default" />
<img data-src="http://p0.meituan.net/movie/59/2366463.jpg@160w_220h_1e_1c" alt="喜剧之王" class="board-img" />
</a>
解决方案:
a_href = soup.select('#app > div > div > div > dl > dd > a >img')
lianjies = []
for a in a_href:
lianjies.append(a.select('img')[-1])