第一个 爬虫和测试

1.爬虫代码如下:

import requests
def gerHTMLText(url):
    try:
        r=requests.get(url,timeout=1)
        r.raise_for_status()
        r.encoding='utf_8'
        return r.text
    except:
        return"访问错误"
url="http://www.google.com"
for i in range (0,20):
    print(gerHTMLText(url))

运行结果如下:

runfile('C:/Users/admin/.spyder-py3/temp.py', wdir='C:/Users/admin/.spyder-py3')
访问错误
访问错误
访问错误
访问错误
访问错误
访问错误
访问错误
访问错误
访问错误
访问错误
访问错误
访问错误
访问错误
访问错误
访问错误
访问错误
访问错误
访问错误
访问错误
访问错误
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 可以使用 Python 的第三方库 `requests` 和 `BeautifulSoup` 来编写一个爬虫。 首先,需要安装这两个库,可以使用 `pip` 命令来安装: ``` pip install requests pip install beautifulsoup4 ``` 然后,可以使用 `requests` 库来发送 HTTP 请求,获取网页内容。例如: ```python import requests url = 'http://example.com' response = requests.get(url) html = response.text ``` 接着,可以使用 `BeautifulSoup` 库来解析 HTML 文本,提取你感兴趣的信息。例如: ```python from bs4 import BeautifulSoup soup = BeautifulSoup(html, 'html.parser') # 提取所有的链接 links = soup.find_all('a') for link in links: print(link.get('href')) ``` 完整的爬虫代码可能长这样: ```python import requests from bs4 import BeautifulSoup def crawl(url): # 发送 HTTP 请求,获取响应内容 response = requests.get(url) html = response.text # 解析 HTML,提取信息 soup = BeautifulSoup(html, 'html.parser') links = soup.find_all('a') # 处理每个链接 for link in links: href = link.get('href') # 对链接进行处理,提取有效信息 print(href) if __name__ == '__main__': crawl('http://example.com') ``` 注意:在编写爬虫时,要遵守网站的 robots.txt 文件,不要对网站进行滥用或者恶意攻击。 ### 回答2: 用Python编写一个爬虫非常简单,主要使用的是Python的 requests 库和 BeautifulSoup 库。 首先,需要安装这两个库,可以通过 pip 命令来安装: ``` pip install requests pip install beautifulsoup4 ``` 导入所需的库: ```python import requests from bs4 import BeautifulSoup ``` 然后,选择需要爬取的网站,并使用 requests 库发送HTTP请求获取网页的内容: ```python url = 'http://example.com' # 替换成要爬取的网址 response = requests.get(url) content = response.text ``` 接下来,使用 BeautifulSoup 库解析网页内容,并提取所需的数据: ```python soup = BeautifulSoup(content, 'html.parser') # 通过选择器定位到需要的元素 elements = soup.select('.class-name') # 通过类名选择器定位 # 获取元素内容 for element in elements: print(element.text) ``` 这里的 '.class-name' 是一个示例,可以根据实际需要修改选择器来定位元素。 最后,可以将所需的数据保存到本地文件或者进行其他处理。 以上就是用 Python 编写爬虫的基本流程,当然还有更多的功能和技巧可以学习和应用。 ### 回答3: 写一个简单的python爬虫可以用到requests库和BeautifulSoup库。 首先,我们需要安装所需的库。在终端中运行以下命令: ``` pip install requests pip install BeautifulSoup4 ``` 接下来,我们将编写一个简单的爬虫程序来获取指定网页的标题。以下是示例代码: ```python import requests from bs4 import BeautifulSoup def get_page_title(url): try: # 发送GET请求获取网页内容 response = requests.get(url) # 使用BeautifulSoup解析网页内容 soup = BeautifulSoup(response.content, 'html.parser') # 获取网页标题 title = soup.title.string return title except requests.exceptions.RequestException as e: print(e) # 测试程序 url = 'https://www.example.com' page_title = get_page_title(url) print('网页标题: ', page_title) ``` 在上面的代码中,我们定义了一个`get_page_title()`函数,它接受一个URL作为参数,并返回该网页的标题。使用`requests.get()`函数发送GET请求来获取网页内容,然后使用BeautifulSoup库解析网页内容,最后使用`soup.title.string`获取网页的标题。 我们可以将要爬取的网页URL传递给`get_page_title()`函数,然后打印出网页标题。 这只是一个简单的示例,实际的爬虫程序可能需要更多的功能和处理。但是这个示例可以作为一个起点来学习如何使用Python编写一个简单的爬虫程序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值