Python之抓取网页元素
import urllib.request
from bs4 import BeautifulSoup
url = "http://www.wal-martchina.com/walmart/store/14_hubei.htm"
user_agent = "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.134 Safari/537.36"
request = urllib.request.Request(url)
request.add_header("User-Agent", user_agent)
content = urllib.request.urlopen(request)
soup = BeautifulSoup(content,from_encoding="gb18030")
#店名
shopname = soup.find_all('td', {"class": "xl714445"})
#地址
addresss = soup.find_all('td', {"class": "xl684445"})
#联系电话
phones = soup.find_all('td', {"class": "xl744445"})
for shop in shopname:
print("店铺名称:"+shop.text.lstrip().rstrip())
print("----------------------------------------------")
for address in addresss:
print("店铺地址:"+address.text.lstrip().rstrip())
sum = 0
for phone in phones:
if sum % 2 == 0:
print("联系电话:" + phone.text.lstrip().rstrip())
else:
print("交通路线:" + phone.text.lstrip().rstrip())
print('---------------------------------------------------')
sum += 1
Python之抓取网页元素的更多相关文章
- python 处理抓取网页乱码
python 处理抓取网页乱码问题一招鲜 相信用python的人一定在抓取网页时,被编码问题弄晕过一阵 前几天写了一个测试网页的小脚本,并查找是否包含指定的信息. 在html = urllib2. ...
- python 解决抓取网页中的中文显示乱码问题
关于爬虫乱码有很多各式各样的问题,这里不仅是中文乱码,编码转换.还包括一些如日文.韩文 .俄文.藏文之类的乱码处理,因为解决方式是一致的,故在此统一说明. 网络爬虫出现乱码的原因 源网页编码和爬取下来 ...
- python分布式抓取网页
呵呵,前两节好像和python没多大关系..这节完全是贴代码, 这是我第一次写python,很多地方比较乱,主要就看看逻辑流程吧. 对于编码格式确实搞得我头大..取下来页面不知道是什么编码,所以先找c ...
- python 处理抓取网页乱码问题一招鲜
FROM: http://my.oschina.net/012345678/blog/122355 相信用python的人一定在抓取网页时,被编码问题弄晕过一阵 前几天写了一个测试网页的小脚本,并查找 ...
- python多线程抓取网页信息
#!/usr/env python #-*- coding: utf-8 -*- import urllib import urllib2 import random import requ ...
- python 简单抓取网页并写入excel实例
# -*- coding: UTF-8 -*- import requests from bs4 import BeautifulSoup import xlwt import time #获取第一页 ...
- (转)用python实现抓取网页、模拟登陆
涉及一系列内容,部分已在前面转载,仍转自crifan: http://www.crifan.com/how_to_use_some_language_python_csharp_to_implemen ...
- python多线程实现抓取网页
Python实现抓取网页 以下的Python抓取网页的程序比較0基础.仅仅能抓取第一页的url所属的页面,仅仅要预定URL足够多.保证你抓取的网页是无限级别的哈,以下是代码: ##coding:utf ...
- Python爬虫抓取东方财富网股票数据并实现MySQL数据库存储
Python爬虫可以说是好玩又好用了.现想利用Python爬取网页股票数据保存到本地csv数据文件中,同时想把股票数据保存到MySQL数据库中.需求有了,剩下的就是实现了. 在开始之前,保证已经安装好 ...
随机推荐
- Infix to Prefix conversion using two stacks
Infix : An expression is called the Infix expression if the operator appears in between the operands ...
- 初学SpringBoot遇到的坑和笔记
目录 1.日期返回给前端显示不理想问题 1.1重现 1.2原因 1.3解决 1.4结果 2.MyBatis-Plus数据库字段未找到问题 2.1重现 2.2原因 2.3解决 3.Long型雪花主键返回 ...
- 输出1-n的全排列dfs
https://ac.nowcoder.com/acm/contest/998/C #include<stdio.h> #include<iostream> #include ...
- PAT A1020 Tree Traversals(25)
题目描述 Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder ...
- find_in_set使用
FIND_IN_SET(str,strList) str 要查询的字符串 strList 字段名,参数以“,”分隔,如(1,2,6,8) 查询字段(strList)中包含的结果,返回结果null或记录 ...
- python 基础(十八)--shutil模块
shutil模块 shutil.copyfileobj(src,dst):只拷贝文件内容,需要open文件:目标文件不存在时创建,存在时覆盖 shutil.copyfileobj(open('old. ...
- 简易计算器-leetcode
今天,开始在leetcode上面开始做题,第一个题目是: Implement a basic calculator to evaluate a simple expression string. Th ...
- MySQL存储引擎InnoDB大量数据下的问题
MySQL如果只有MyISAM一个引擎的话,那你们黑真的也有道理,但问题是InnoDB现在已经是MySQL默认的引擎,而且这个引擎综合能力很强,能用好这个引擎其实就已经能解决大多数需要数据库的业务逻辑 ...
- isEmpty 和 isBlank 区别
isEmpty 和 isBlank 区别 org.apache.commons.lang.StringUtils 类提供了 String 的常用操作,最为常用的判空有如下两种 isEmpty(Stri ...
- lua加载DLL
.cpp //若没有在项目属性--库文件.依赖文件.包含添加.则添加一下路径 #pragma comment (lib,"lua5.1.lib") #include " ...