用这种方式r'<ul><a href=.*?>(.*?)</a></ul>'爬取https://www.ntst.edu.cn/主页面通知通告部分
时间: 2024-05-28 08:03:32 浏览: 18
可以使用Python的requests和re库来实现爬取,具体代码如下:
```python
import requests
import re
url = 'https://www.ntst.edu.cn/'
html = requests.get(url).text
notices = re.findall(r'<ul><a href=.*?>(.*?)</a></ul>', html)
for notice in notices:
print(notice)
```
这段代码首先使用requests库获取网页的html代码,然后使用正则表达式r'<ul><a href=.*?>(.*?)</a></ul>'匹配出通知通告部分的内容,最后将结果打印出来。
相关问题
一个<div>里面有两个一样的<ul>,两个<ul>的class和id也相同,我知道第二个<ul>里面的一个text,爬取第二个<ul>的全部超链接
可以使用BeautifulSoup库来解析HTML文档,代码如下:
```python
from bs4 import BeautifulSoup
html_doc = """
<html>
<head>
<title>Test Page</title>
</head>
<body>
<div>
<ul class="list" id="list1">
<li><a href="http://example.com/page1">Page 1</a></li>
<li><a href="http://example.com/page2">Page 2</a></li>
</ul>
<ul class="list" id="list1">
<li><a href="http://example.com/page3">Page 3</a></li>
<li><a href="http://example.com/page4">Page 4</a></li>
</ul>
</div>
</body>
</html>
"""
soup = BeautifulSoup(html_doc, 'html.parser')
# 找到第二个<ul>标签
ul_tag = soup.find_all('ul', class_='list', id='list1')[1]
# 找到全部超链接
links = ul_tag.find_all('a')
# 输出超链接
for link in links:
print(link.get('href'))
```
输出结果为:
```
http://example.com/page3
http://example.com/page4
```
爬取两个一样的<ul>里的超链接用selenium
可以按照如下步骤使用Selenium来爬取两个一样的`<ul>`标签中的超链接:
1. 首先,使用Selenium打开一个网页,例如:
```python
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://www.example.com")
```
2. 接下来,找到两个`<ul>`标签的XPath,例如:
```python
first_ul_xpath = "//ul[@class='first-ul']"
second_ul_xpath = "//ul[@class='second-ul']"
```
这里假设第一个`<ul>`标签的class属性为`first-ul`,第二个`<ul>`标签的class属性为`second-ul`。
3. 然后,使用`find_elements_by_xpath`方法找到两个`<ul>`标签下的所有`<a>`标签,例如:
```python
first_links = driver.find_elements_by_xpath(first_ul_xpath + "//a")
second_links = driver.find_elements_by_xpath(second_ul_xpath + "//a")
```
4. 最后,遍历两个列表,获取每个超链接的URL和文本内容,例如:
```python
for link in first_links:
url = link.get_attribute("href")
text = link.text
print(url, text)
for link in second_links:
url = link.get_attribute("href")
text = link.text
print(url, text)
```
这里使用`get_attribute`方法获取超链接的`href`属性,使用`text`属性获取超链接的文本内容。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
# 目标网站:www.hongniuziyuan.com # 爬取下来的数据格式 # 保存到记事本中 # 电影名字|导演|演员|评分|所属地区|播放地址 # 变形金刚|小斯蒂芬·卡普尔|安东尼·拉莫斯,多米尼克·菲什巴克,彼特·库伦,朗·普尔曼,彼特·丁拉|0.0|美国|https://hnzy.bfvvs.com/play/YervnY4e import requests from lxml import etree response = requests.get("https://www.hongniuziyuan.com/") html = response.content.decode() # 将二进制数据转换为文本数据 # response.text 文本数据 # print(html) # 将我们字符串转换为一个dom树 dom = etree.HTML(html) # 目的就是为了使用xpath语法去寻找我们的想要的标签 # xpath语法 # xs = dom.xpath('/html/body/div[3]/div[3]/ul/div/li/div/a/img') xs = dom.xpath('/html/body/div[4]/ul[2]/li/strong/span[1]/a') #元素:妖神记 第五季 [第328集完结] #xpath:/html/body/div[4]/ul[2]/li/strong/span[1]/a for s in xs:
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)